链接隐藏,直到滚动使用jquery
link hidden until scroll using jquery
我有一个back to top的链接但是即使页面在顶部它也会显示
<a class="w-toplink active" href="#"><i class="fa fa-chevron-up"></i></a>
我希望它被隐藏,直到用户开始滚动页面-这是可能的吗?如果有,怎么做?
另一件事,我已经尝试并试图做的是使链接改变悬停与不透明,但它似乎没有工作-谁能看到我哪里出了问题。
CSS.w-toplink {
display: block;
position: fixed;
bottom: -50px;
right: 30px;
text-align: center;
font-size: 14px;
padding-top:15px;
line-height: 50px;
height: 30px;
width: 50px;
border-radius: 5px;
opacity: 0;
z-index: 100;
-webkit-transition: background-color 0.3s, opacity 0.3s, bottom 0.3s;
transition: background-color 0.3s, opacity 0.3s, bottom 0.3s;
background-color: #333;
background-color: rgba(0, 0, 0, 0.3);
color: #fff;
}
.w-toplink.active {
bottom: 30px;
opacity: 0.7;
}
.w-toplink:hover {
opacity: 0.7;
}
jQuery:
$(window).scroll(function(){
var scrollTop = $(window).scrollTop(),
$toplink = $('.w-toplink');
if(scrollTop > 0){
$toplink.addClass('active');
} else{
$toplink.removeClass('active');
}
});
CSS: .w-toplink{opacity:0; transition: opacity 200ms ease-in;}
.w-toplink.active{opacity:0.7;}
.w-toplink:hover{opacity:1;}
编辑我已经为你添加了一些额外的代码。
- 。w-toplink默认隐藏
- 当用户滚动时,它的不透明度变为0.7(通过类。active)
- 当用户将链接悬停时,它变得完全可见(不透明度:1)
这样做:
$(window).scroll(function () {
if ($(this).scrollTop() > 0) {
$('.w-toplink').fadeIn(250);
} else {
$('.w-toplink').fadeOut(250);
}
});
并添加到你的CSS文件:
.w-toplink {
display: none;
}
听起来很有趣…
有可能你必须简单地为滚动放置一个eventListener。在vanillaJS或jQuery中是可能的…
查看更多关于scroll的信息:https://api.jquery.com/scroll/
<a class="w-toplink active" href="#"><i class="fa fa-chevron-up"></i></a>
(假设您已经在使用jQuery CDN。
<script type="text/javascript">
$("a.w-toplink").hide();
$( window ).scroll(function() {
$( "a.w-toplink" ).css( "display", "visible" ).fadeIn( "slow" );
});
$("a.w-toplink").hide();
$(window).scroll(function() {
$("a.w-toplink").show();
});
.w-toplink {
display: block;
position: fixed;
bottom: 0;
right: 30px;
text-align: center;
font-size: 14px;
padding-top:15px;
line-height: 50px;
height: 30px;
width: 50px;
border-radius: 5px;
z-index: 100;
-webkit-transition: background-color 0.3s, opacity 0.3s, bottom 0.3s;
transition: background-color 0.3s, opacity 0.3s, bottom 0.3s;
background-color: #333;
background-color: rgba(0, 0, 0, 0.3);
color: #fff;
}
<a href="#" class="w-toplink stuff">Back to to top</a>
<p>Lorem Ipsum and stuff...Lorem Ipsum and stuff... Lorem Ipsum and stuff... Lorem Ipsum and stuff... Lorem Ipsum and stuff...Lorem Ipsum and stuff...Lorem Ipsum and stuff... Lorem Ipsum and stuff... Lorem Ipsum and stuff...Lorem Ipsum and stuff...Lorem
Ipsum and stuff...Lorem Ipsum and stuff... Lorem Ipsum and stuff... Lorem Ipsum and stuff...
</p>
<ul>
<li>Sample</li>
<li>Sample</li>
<li>Sample</li>
<li>Sample</li>
<li>Sample
<p>Lorem Ipsum and stuff...Lorem Ipsum and stuff... Lorem Ipsum and stuff... Lorem Ipsum and stuff... Lorem Ipsum and stuff...Lorem Ipsum and stuff...Lorem Ipsum and stuff... Lorem Ipsum and stuff... Lorem Ipsum and stuff...Lorem Ipsum and stuff...Lorem
Ipsum and stuff...Lorem Ipsum and stuff... Lorem Ipsum and stuff... Lorem Ipsum and stuff...
</p>
</li>
<li>Sample</li>
<li>Sample</li>
<li>Sample</li>
<li>Sample</li>
<li>Sample
<p>Lorem Ipsum and stuff...Lorem Ipsum and stuff... Lorem Ipsum and stuff... Lorem Ipsum and stuff... Lorem Ipsum and stuff...Lorem Ipsum and stuff...Lorem Ipsum and stuff... Lorem Ipsum and stuff... Lorem Ipsum and stuff...Lorem Ipsum and stuff...Lorem
Ipsum and stuff...Lorem Ipsum and stuff... Lorem Ipsum and stuff... Lorem Ipsum and stuff...
</p>
</li>
<li>Sample</li>
<li>Sample</li>
<li>Sample</li>
<li>Sample</li>
<li>Sample</li>
<li>Sample</li>
<li>Sample</li>
<li>Sample</li>
<li>Sample</li>
</ul>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</script>
相关文章:
- jQuery滚动功能只工作一次
- jQuery滚动器插件修改
- 当元素可见时,jQuery滚动函数会触发一次
- jQuery滚动到聊天框底部
- 如何发现用户是否使用jQuery滚动到HTML容器的末尾
- 使用 jQuery 滚动窗格
- jQuery 滚动到锚点适用于移动设备,但不适用于桌面
- JQuery 滚动列表框模仿键盘导航
- JQuery 滚动到锚点行为奇怪
- jquery滚动功能无法正常工作
- JQuery滚动到底部
- 使用jQuery滚动获取距窗口顶部的距离
- jQuery滚动&锚初始位置
- 如何使用jQuery滚动到页面顶部
- 核心滚动标题面板+jQuery滚动
- iOS jquery滚动视图插件不会在iframe上滚动y
- 取消弹出jquery滚动事件
- jQuery滚动力不会滚动超过450px
- jQuery滚动出现问题
- Jquery滚动到基于数据属性的部分