需求很典型:點擊導航欄展開子選單,但點擊頁面其他空白處時要自動收起。行為幾乎和 Bootstrap 的 dropdown 一樣,只是樣式要配合設計稿,所以不能」。直接套用元件,得自己寫。
最直覺的想法是:幫 body 綁一個 click 事件,點空白處就收合選單。聽起來合理,但一試就壞——點擊觸發器本身的時候,事件會冒泡到 body,選單剛展開就被收回去,等於永遠打不開。
既然 Bootstrap 已經解決過這個問題,就去讀它的原始碼。關鍵在於兩件事的組合:把收合事件綁在 document 上,並在觸發器的事件裡阻止冒泡。這樣點觸發器時事件不會往上傳,點其他地方時才會被 document 攔下。
<!-- 篩選導航欄 -->
<div class="border_b_bottom_3eee text-center width_40 float_left active" style="z-index: 999">
<div class="margin_bottom_10 margin_top_10">
<span onclick="showOrHideItem(this,event)" class="title">
分類 <span class="caret"></span>
</span>
<ul class="list-unstyled all_width sqh_absolute sqh_line_height_25 sqh_tmp_bj_ff
border_b_bottom_eee sqh_position_top_100 sqh_position_left_0 display_none"
data-show="hide" style="z-index: 999;">
<li class="margin_left_10 margin_right_10 sqh_pointer border_b_bottom_eee"
onclick="jumpTo(this)" target="https://www.example.com">
<span class="float_left">家政</span>
<span class="float_right icon iconfont sqh_line_height_15"></span>
<span class="clearfix"></span>
</li>
<li class="margin_left_10 margin_right_10 sqh_pointer border_b_bottom_eee"
onclick="jumpTo(this)" target="https://www.example.com">
<span class="float_left">蔬菜</span>
<span class="float_right icon iconfont sqh_line_height_15"></span>
<span class="clearfix"></span>
</li>
<li class="margin_left_10 margin_right_10 sqh_pointer sqh_font_color_red"
onclick="jumpTo(this)" target="https://www.example.com">
<span class="float_left">零食</span>
<span class="float_right icon iconfont sqh_line_height_15"></span>
<span class="clearfix"></span>
</li>
</ul>
</div>
</div>
<!-- 搜尋框 -->
<div class="text-right border_b_bottom_3eee text-center width_20 float_left">
<div class="margin_bottom_10 margin_top_10" onclick="showSearch(this,event)">
<span class="icon iconfont font_14 display_block padding_left_5"></span>
</div>
<div class="sqh_tmp_bj_ff">
<div class="sqh_absolute sqh_line_height_25 sqh_tmp_bj_ff search_cont"
style="top:58%;right: 0px;z-index: 999;display: none;" data-search="hide">
<div class="margin_left_15">
<div class="sqh_relative" style="margin-right: 80px;">
<span class="icon iconfont font_14 sqh_absolute padding_left_5"
style="left: 0px;top:0px;"></span>
<input class="in_search all_width padding_left_30 sqh_tmp_bj_f3 sqh_border_radius_20"
placeholder="搜索" onclick="stopEvent(this,event)" type="text" value="">
</div>
<div class="float_right" style="width: 80px;">
<button type="button" class="btn btn-e4005a" style="padding: 4px 12px;">搜索</button>
</div>
</div>
</div>
</div>
</div>
<script>
$(function(){
// 收合下拉選單:綁在 document 上
$(document).on("click", function(){
$("ul[data-show='show']").slideUp("slow");
});
// 收合搜尋框
$(document).on("click", function(){
$("div[data-search='show']").css("display","none").css("width","32%");
});
});
// 展開或收合篩選條件
function showOrHideItem(obj, event){
var $currentObj = $(obj);
// 先收合所有已展開的清單
$("ul[data-show='show']").hide();
// 清掉所有 active 樣式
$currentObj.closest(".row").find("div.active").removeClass("active");
// 給當前項目加上選中樣式
$currentObj.closest(".float_left").addClass("active");
var $ul = $currentObj.closest("div").find("ul");
if ($ul.data("show") == "show") {
$ul.slideUp("slow");
$ul.attr("data-show", "hide");
} else {
$ul.slideDown("slow");
$ul.attr("data-show", "show");
// 關鍵:阻止事件冒泡到 document
event.stopPropagation();
}
}
// 展開搜尋框
function showSearch(obj, event){
var $currentObj = $(obj).closest(".float_left")
.find(".search_cont").css("display", "block");
$currentObj.animate({ width: "100%" }, 1000);
$currentObj.attr("data-search", "show");
event.stopPropagation();
}
function stopEvent(obj, event){
event.stopPropagation();
}
</script>
第一,用 data-show="hide" 這種自訂屬性來記錄展開狀態,比依賴 class 或記憶體變數更直觀,也方便除錯時直接從 DOM 看出狀態。
第二,搜尋框的 input 上也綁了 stopEvent,這是必要的——不然點擊輸入框打字時,事件會冒泡到 document,把剛展開的搜尋框收掉。
第三,stopPropagation() 只阻擋冒泡,不會阻擋其他同層事件;若想連預設行為一起擋掉,才需要 preventDefault()。這兩者用途不同,別混用。
理解事件冒泡與其攔截方式,是自製 dropdown、modal、tooltip 這類互動元件的共同基礎。希望對你的 JavaScript 開發有所幫助。