Ontouchstart删除悬浮在iPad iphone

ontouchstart remove ahover in ipad iphone

本文关键字:iPad iphone 悬浮 删除 Ontouchstart      更新时间:2023-09-26

我有一个按钮有悬停效果。然而,我想禁用悬浮在ipad和iphone。它不工作

$(document).ready(function () {
            $(".content").hide();
            $(".open1").click(function () {
                $(this).next().slideToggle(400);
                if('ontouchstart' in document){$('#btn_01').unbind('mouseenter mouseleave');}
                var btn = $('#btn_01'), isOn = btn.hasClass('on');              
                if(isOn) { btn.removeClass('on'); }
                else if(btn) { btn.addClass('on'); }
                });
        });
<style type="text/css">
    #btn_01{width:510px; height:109px; background:url('images/btn_01_on.png') no-repeat;}
        #btn_01:hover{width:510px; height:109px; background:url('images/btn_01_over.png') no-repeat;}
        #btn_01.on{width:510px; height:109px; background:url('images/btn_01_over.png') no-repeat;}

我会采用不同的方法:

jQuery(function($) {
    var className = ('ontouchstart' in document ) ? "touch-device" : "desktop-device";
    $(document.body).addClass(className);
});

然后你可以使用body.desktop-devicebody.touch-device来指定样式的调整:

body.desktop-device #btn_01:hover {
    background: red;
}
body.touch-device #btn_01 {
    background: blue;
}

var a = b ? c : d;
//means:
if ( b ) {
    var a = c;
}
else {
    var a = d;
}

找到了一个非常简单的方法来解决这个问题,使用CSS @media我将hover的图像更改为原始图像。

#btn_01{btn_01_on.png'}
#btn_01:hover{btn_01_on.png'}
#btn_01.on{btn_01_over.png'}

@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : landscape) {
#btn_01{width:510px; height:109px; background:url('images/btn_01_on.png') no-repeat;}
#btn_01:hover{width:510px; height:109px; background:url('images/btn_01_on.png') no-repeat;}
#btn_01.on{width:510px; height:109px; background:url('images/btn_01_over.png') no-repeat;}