I implemented "scroll to top" arrow from this codepen之后在手机上悬停:
jQuery:
// ===== Scroll to Top ====
$(window).scroll(function() {
if ($(this).scrollTop() >= 50) { // If page is scrolled more than 50px
$('#return-to-top').fadeIn(200); // Fade in the arrow
} else {
$('#return-to-top').fadeOut(200); // Else fade out the arrow
}
});
$('#return-to-top').click(function() { // When arrow is clicked
$('body,html').animate({
scrollTop : 0 // Scroll to top of body
}, 500);
});
CSS:
#return-to-top {
position: fixed;
bottom: 20px;
right: 20px;
background: rgb(0, 0, 0);
background: rgba(0, 0, 0, 0.7);
width: 50px;
height: 50px;
display: block;
text-decoration: none;
-webkit-border-radius: 35px;
-moz-border-radius: 35px;
border-radius: 35px;
display: none;
-webkit-transition: all 0.3s linear;
-moz-transition: all 0.3s ease;
-ms-transition: all 0.3s ease;
-o-transition: all 0.3s ease;
transition: all 0.3s ease;
}
#return-to-top i {
color: #fff;
margin: 0;
position: relative;
left: 16px;
top: 13px;
font-size: 19px;
-webkit-transition: all 0.3s ease;
-moz-transition: all 0.3s ease;
-ms-transition: all 0.3s ease;
-o-transition: all 0.3s ease;
transition: all 0.3s ease;
}
#return-to-top:hover {
background: rgba(0, 0, 0, 0.9);
}
#return-to-top:hover i {
color: #fff;
top: 5px;
}
在台式机上运行良好,但在手机屏幕上存在问题。单击箭头并且屏幕滚动到顶部时,如果再次向下滚动,箭头将显示为:hover状态。
点击后如何“取消”悬停?
编辑:
以下是显示问题的截屏视频:https://streamable.com/5wr27
答案 0 :(得分:0)
只需尝试 javascript:
<a onclick="this.blur();">Top</a>
OR
$('#return-to-top').click(function() {
$(this).blur();
$('body,html').animate({
scrollTop : 0 // Scroll to top of body
}, 500);
});
CSS:
#return-to-top:focus {
outline: none;
}
答案 1 :(得分:0)
我认为您不能在触摸屏上产生:hover效果。
相反,可以在媒体查询中使用:active或:clicked选择器
media only screen and (max-width: 600px) {
return-to-top:active {
background: rgba(0, 0, 0, 0.9);
}
}
也许是这样? 您可以查找合适的宽度来使用。
答案 2 :(得分:0)
我认为正在发生的是:hover始终附加,因此被认为是悬停了。因此,您需要删除悬停。我发现解决方案可以通过添加
$('yourelement').die('click');
希望此解决方案对您有所帮助。
$(window).scroll(function() {
if ($(this).scrollTop() >= 50) { // If page is scrolled more than 50px
$('#return-to-top').fadeIn(200); // Fade in the arrow
} else {
$('#return-to-top').fadeOut(200);
$('a').die('click');
// Else fade out the arrow
}
});
答案 3 :(得分:0)
由于当鼠标离开元素时,您所指的:hover
状态会丢失,因此应用于该状态的所有样式也会丢失,从而将元素的样式恢复为其原始样式。
在移动设备屏幕上不会发生此行为,因为您必须用某些东西来按它,而不仅仅是将鼠标悬停在其上。在这种情况下,元素保留了按下时应用的样式,而不是丢失样式(就像鼠标离开元素时那样)。
您所要做的就是手动将元素恢复为之前的状态,
// ===== Scroll to Top ====
$(window).scroll(function() {
if ($(this).scrollTop() >= 50) { // If page is scrolled more than 50px
$('#return-to-top').fadeIn(200); // Fade in the arrow
} else {
$('#return-to-top').fadeOut(200); // Else fade out the arrow
}
});
$('#return-to-top').click(function() { // When arrow is clicked
$('body,html').animate({
scrollTop: 0 // Scroll to top of body
}, 500);
});
#return-to-top {
position: fixed;
bottom: 20px;
right: 20px;
background: rgb(0, 0, 0);
background: rgba(0, 0, 0, 0.7);
width: 50px;
height: 50px;
display: block;
text-decoration: none;
-webkit-border-radius: 35px;
-moz-border-radius: 35px;
border-radius: 35px;
display: none;
-webkit-transition: all 0.3s linear;
-moz-transition: all 0.3s ease;
-ms-transition: all 0.3s ease;
-o-transition: all 0.3s ease;
transition: all 0.3s ease;
}
#return-to-top i {
color: #fff;
margin: 0;
position: relative;
left: 16px;
top: 13px;
font-size: 19px;
-webkit-transition: all 0.3s ease;
-moz-transition: all 0.3s ease;
-ms-transition: all 0.3s ease;
-o-transition: all 0.3s ease;
transition: all 0.3s ease;
}
#return-to-top:hover {
background: rgba(0, 0, 0, 0.9);
}
#return-to-top:hover i {
color: #fff;
top: 5px;
}
#return-to-top:not(:focus),
#return-to-top:not(:active) {
background: rgba(0, 0, 0, 0.7);
}
#return-to-top:not(:focus) i,
#return-to-top:not(:active) i {
top: 13px
}
/* Extra Things */
body {
background: #eee;
font-family: 'Open Sans', sans-serif;
}
h3 {
font-size: 30px;
font-weight: 400;
text-align: center;
margin-top: 50px;
}
h3 i {
color: #444;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Return to Top -->
<a href="javascript:" id="return-to-top"><i class="icon-chevron-up"></i></a>
<!-- ICON NEEDS FONT AWESOME FOR CHEVRON UP ICON -->
<link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">
<!-- Test the scroll -->
<div style="height:2000px;">
<h3>Scroll down</h3>
<h3><i class="icon-arrow-down"></i>
</div>
您还可以签出this answer以仅将这些样式应用于移动设备。