Last active
June 22, 2017 10:10
-
-
Save alex1504/e3c140478ce02077c151180eda82f3cf to your computer and use it in GitHub Desktop.
jQuery options pop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.extend({ | |
popOptions: function(params) { | |
var defaults = { | |
title: "标题", | |
options: [{ | |
name: "选项一", | |
onClick: function() {} | |
}, | |
{ | |
name: "选项二", | |
onClick: function() {} | |
}, | |
], | |
active: 0, | |
onCancle: function() { | |
console.log('cancle') | |
} | |
} | |
params = $.extend(defaults, params); | |
optionsHtml = params.options.map(function(obj, index) { | |
if (index == params.active) { | |
return "<li class='option active'>" + obj.name + "<span class='right'></span></li>" | |
} else { | |
return "<li class='option'>" + obj.name + "</li>" | |
} | |
}) | |
optionsHtml = optionsHtml.join(''); | |
var tpl = [ | |
"<div class='m-pop-options'>", | |
"<div class='mask'></div>", | |
"<div class='content'>", | |
"<div class='title'>" + params.title + "</div>", | |
"<ul class='options'>" + optionsHtml + "<li class='cancle'>取消</li>", | |
"</ul>", | |
"</div>", | |
"</div>" | |
].join('') | |
var el = $(tpl).appendTo('body'); | |
$(el).find('.option').each(function(index) { | |
$(this).click(function(e) { | |
e.preventDefault(); | |
$(el).remove(); | |
(typeof params.options[index].onClick == 'function') && params.options[index].onClick(params.options[index].name) | |
}) | |
}) | |
$(el).find('.cancle').click(function(e) { | |
e.preventDefault(); | |
$(el).remove(); | |
(typeof params.onCancle == 'function') && params.onCancle(); | |
}) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment