gpt4 book ai didi

javascript - 如果 Body 在 Jquery 中有类,则防止悬停功能

转载 作者:行者123 更新时间:2023-11-27 23:09:25 25 4
gpt4 key购买 nike

我有一个悬停功能,可以在悬停链接时显示和隐藏 div,还有另一个功能可以在一段时间后自动隐藏和显示 div。这很好用。

我想在触摸设备上禁用悬停功能。我使用以下代码在触摸设备上添加“user-is-touching”类:

window.addEventListener('touchstart', function onFirstTouch() {
document.body.classList.add('user-is-touching');
window.USER_IS_TOUCHING = true;
$(".o-c, .c-f, .i-c, .c-u").unbind('mouseenter').unbind('mouseleave')
window.removeEventListener('touchstart', onFirstTouch, false);
}, false);

var homeLinks = ['i-t', 'o-c', 'c-f', 'i-c', 'c-u'];
var currentLink = 0;
var hovered = false;

$(".home-link").hover(function() {
hovered = true;
$('.home-' + homeLinks[currentLink]).hide();
$('[data-hover=' + homeLinks[currentLink] + ']').toggleClass('default-underline');
currentLink = homeLinks.indexOf($(this).attr('data-hover'));
$('[data-hover=' + homeLinks[currentLink] + ']').toggleClass('default-underline');
$('.home-' + homeLinks[currentLink]).show();
}, function() {
hovered = false;
});

var autoNavInterval = setInterval(autoNav, 1000);

function autoNav() {
if (hovered === false) {
$('.home-' + homeLinks[currentLink]).hide();
$('[data-hover=' + homeLinks[currentLink] + ']').toggleClass('default-underline');
currentLink++;
if (currentLink >= homeLinks.length) {
currentLink = 0;
}
$('[data-hover=' + homeLinks[currentLink] + ']').toggleClass('default-underline');
$('.home-' + homeLinks[currentLink]).show();
}
}
.left-fill {
background: #0000006b;
height: 100vh;
}

.right-fill {
background: pink;
height: 100vh;
}

.vc_col-sm-6 {
width: 50%;
float: left;
}

.pivot-nav {
list-style: none;
font-family: 'Montserrat';
text-align: left;
}

.pivot-nav li a {
font-size: 1.6rem;
font-weight: 700;
text-transform: uppercase;
display: inline-block;
position: relative;
color: #fff;
text-decoration: none;
line-height: 40px;
}

.pivot-nav li a.default-underline::after,
.pivot-nav li a:hover::after {
width: 100%;
}

.pivot-nav:hover a.default-underline:not(:hover)::after {
width: 0;
}

.pivot-nav li a::after {
bottom: 0;
content: "";
display: block;
height: 4px;
position: absolute;
background: #fff;
transition: width 0.3s ease 0s;
width: 0;
}

.home-o-c,
.home-c-f,
.home-i-c,
.home-c-u {
display: none;
}

.our-company {
clear: both;
display: block;
height: 50vh;
}

.cf2 {
clear: both;
display: block;
height: 50vh;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="left-fill text-left wpb_column vc_column_container vc_col-sm-6">

<div class="wpb_wrapper">
<p class="home-i-t">TEXT One</p>
<p class="home-o-c">TEXT One</p>
<p class="home-c-f">TExt for C f.</p>
<p class="home-i-c">Some more text fo i c.</p>
<p class="home-c-u">Get in touch </p>

</div>
</div>



<div class="home-fill right-fill text-right wpb_column vc_column_container vc_col-sm-6">

<div class="wpb_wrapper">
<ul class="pivot-nav">
<li class="pivot-nav-item"><a data-hover="o-c" class="home-link" href="#" data-toggle="my-scrollspy-2">O C</a></li>
<li class="pivot-nav-item"><a data-hover="c-f" class="home-link" href="#" data-toggle="my-scrollspy-2">C F</a></li>
<li class="pivot-nav-item"><a data-hover="i-c" class="home-link" href="#" data-toggle="my-scrollspy-2">I C</a></li>
<li class="pivot-nav-item" data-toggle="my-scrollspy-2"><a data-hover="c-u" class="home-link" href="#">C U</a></li>
</ul>

</div>
</div>

最佳答案

与其等待第一个“触摸事件”来确定它是否是触摸设备,不如这样检查:

var is_touch = 'ontouchstart' in document.documentElement;


if(!is_touch){
$(".home-link").hover(function() { ... });

}

关于javascript - 如果 Body 在 Jquery 中有类,则防止悬停功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58608269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com