
如何实现这个hover事件呢,其实在dropdown组件的点击事件的基础上很好完成的。细心者可以发现,下拉框出现时,其父级会有一个open的class属性。我们只需要监听hover事件时,给父级增加或删除open类就可以了。
boostrap-hover-dropdown.js插件,托管在github上的代码网址:查看
下面是完整的js插件代码:
// bootstrap响应式导航条<br>;(function($, window, undefined) {// outside the scope of the jQuery plugin to// keep track of all dropdownsvar $allDropdowns = $(); // if instantlyCloseOthers is true, then it will instantly// shut other nav items when a new one is hovered over$.fn.dropdownHover = function(options) { // the element we really care about// is the dropdown-toggle"s parent$allDropdowns = $allDropdowns.add(this.parent()); return this.each(function() {var $this = $(this).parent(),defaults = {delay: 500,instantlyCloseOthers: true},data = {delay: $(this).data("delay"),instantlyCloseOthers: $(this).data("close-others")},options = $.extend(true, {}, defaults, options, data),timeout; $this.hover(function() {if(options.instantlyCloseOthers === true)$allDropdowns.removeClass("open"); window.clearTimeout(timeout);$(this).addClass("open");}, function() {timeout = window.setTimeout(function() {$this.removeClass("open");}, options.delay);});});}; $("[data-hover="dropdown"]").dropdownHover();})(jQuery, this);可以看到作者在插件前面加了个分号;,增加了插件的兼容性,因为可能上一个js代码没写;,如果在此不加分号则可能因为没换行导致js出错。
以上就是为大家分享的Bootstrap dropdown组件扩展hover事件使用方法,希望对大家熟练掌握hover事件有所帮助。