jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jQuery悬停功能超时大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用下面的代码使用jQuery和悬停功能用户将鼠标悬停在图像上时淡入’caption’元素.这在桌面浏览器上非常有效,但是当使用移动触摸设备(如iPad)对其进行测试时,需要用户点击图像来触发悬停功能,标题会按预期淡入,但在用户选择页面上的其他对象之前保持活动状态.

我想知道一种简单的方法修改我的javascript代码以检测移动触摸设备,然后将一些排序或计时器放到标题中,以便它在一段时间后自动消失?

<!-- include jQuery library -->
<script type="text/javascript" src="./_js/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {

    //On mouse over those thumbnail
    $('.item-caption').hover(function() {

        //Display the caption
    $(this).find('.caption').stop(false,true).fadeIn(200);
    },function() {    

    //Hide the caption
    $(this).find('.caption').stop(false,true).fadeOut(600);
});

});
</script>

</head>
<body>

    <div class="item-caption"><a href="http://www.domain.com">
        <div class="caption">   
            <ul>
                <li><h2>TITLE</h2></li>
                <li><h3>Description</h3></li>
            </ul>       
        </div>
        <img src="./_img/example_image.jpg"></a>
    </div>

</body>

任何想法,想法都会受到最高的赞赏.

克里斯

解决方法

您可以检测具有特征检测的触摸设备,然后使用延时的fadeOut()相应地调整您的行为:

$(document).ready(function() {

    function hasTouch() {
        try {
           document.createEvent("TouchEvent");
           return(true);
        } catch (e) {
           return(false);
        }    
    }
    var touchPresent = hasTouch();

    //On mouse over those thumbnail
    $('.item-caption').hover(function() {

        //Display the caption
        var caption = $(this).find('.caption');
        caption.stop(true,true).fadeIn(200);
        // on touch systems,fade out after a time delay
        if (touchPresent) {
            caption.delay(5000).fadeOut(600);
        }
    },function() {    

        //Hide the caption
        $(this).find('.caption').stop(true,true).fadeOut(600);
    });

});

大佬总结

以上是大佬教程为你收集整理的jQuery悬停功能超时全部内容,希望文章能够帮你解决jQuery悬停功能超时所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: