Bootstrap中的Dropdown下拉菜單更改為懸停(hover)觸發(fā)
在使用bootstrap制作后臺時用到了響應式導航條,其中dropdown組件更是用的比較多,用的多需要點擊的就多,dropdown默認鼠標左鍵單擊才展開,如果使用鼠標放上去(hover)就展開則會省去點擊時間,這樣能提高效率。

原本的改造思路是:給dropdown元素綁定hover事件,hover上去的時候,執(zhí)行該元素的click事件——即把hover同步為click,hover即為click。
但想到與其自己來改造,不如先在網(wǎng)上搜索搜索看看有沒有現(xiàn)成的插件,果不其然就搜索到了,托管在github上的代碼網(wǎng)址:查看
在這兒就直接把代碼復制出來:
;(function($, window, undefined) {
// outside the scope of the jQuery plugin to
// keep track of all dropdowns
var $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出錯。
插件支持HTML元素data-*傳參,也支持初始化傳參。將此js代碼放在bootstrap原本的js代碼后面執(zhí)行即可。
以上所述是小編給大家介紹的Bootstrap中的Dropdown下拉菜單更改為懸停(hover)觸發(fā),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- BootStrap中關于Select下拉框選擇觸發(fā)事件及擴展
- Bootstrap select多選下拉框?qū)崿F(xiàn)代碼
- bootstrap下拉列表與輸入框組結合的樣式調(diào)整
- Bootstrap每天必學之下拉菜單
- Bootstrap模塊dropdown實現(xiàn)下拉框響應
- Bootstrap框架下下拉框select搜索功能
- Bootstrap Table 在指定列中添加下拉框控件并獲取所選值
- Bootstrap每天必學之級聯(lián)下拉菜單
- bootstrap中selectpicker下拉框使用方法實例
- IE11下CKEditor在Bootstrap Modal中下拉問題的解決
相關文章
js和jquery判斷數(shù)據(jù)類型的4種方法總結
這篇文章主要給大家介紹了關于js和jquery判斷數(shù)據(jù)類型的4種方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08

