達令說
2019-02-12 17:12:30
我要在移動端實現(xiàn)點擊按鈕顏色變化的(類似pc的hover效果)效果,點擊后顏色變化停留一秒鐘,然后變回去?第一種方法var icon_timer;$('.nav li .glyphicon-menu-down').on('click', function(e) { if ($(window).width() < 991) { $(this).addClass('touch-hov'); clearTimeout(icon_timer); icon_timer = setTimeout(function() {$(this).removeClass('touch-hov')},1000); }});發(fā)現(xiàn)settimeout中的的removeclass總是不執(zhí)行?第二種方法$('.nav li>div').on('touchstart click', function(e) { if ($(window).width() < 991) { e.stopPropagation(); $(this).addClass('touch-hov'); clearTimeout(div_timer); }});$('.nav li>div').on('touchend', function() { div_timer = setTimeout(function() {$(this).removeClass('touch-hov')},1000);});使用這種方法有時不進入touchend事件,進去了 setTimeout也不執(zhí)行?不知道是什么原因造成的?
2 回答

12345678_0001
TA貢獻1802條經(jīng)驗 獲得超5個贊
為何不用CSS3做?
根據(jù)你的需求,add class之后CSS3的animation以及transition 是完全可以實現(xiàn)你想要的效果
參考 CSS3 transition 以及 Animation屬性
--update2
至于你的問題,屬于this指向問題,你可以自己debugger看看this是什么

滄海一幻覺
TA貢獻1824條經(jīng)驗 獲得超5個贊
this綁定問題
$('.nav li .glyphicon-menu-down').on('click', function(e) {
var $el = $(this);
if ($(window).width() < 991) {
$(this).addClass('touch-hov');
clearTimeout(icon_timer);
icon_timer = setTimeout(function() {$el.removeClass('touch-hov')},1000);
}
});
添加回答
舉報
0/150
提交
取消