简体   繁体   English

如何在滚动时显示侧边栏,并在页面顶部将其隐藏?

[英]How to show a sidebar when scrolling and hide it when at the top of the page?

This is my sidebar: 这是我的侧边栏:

<ul id="sidebarupcoming">
<li><a href="#week36">WEEK 36 </a></li>
<li><a href="#week37">WEEK 37 </a></li>
<li><a href="#week38">WEEK 38 </a></li>
</ul>

And I tried to hide it with this script 我试图用这个脚本隐藏它

<!– Scroll –>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="http://static.tumblr.com/fgfmpgh/Qqfkzalyi/jquery.scroll.pack.js"></script>
<script type="text/javascript" src="http://static.tumblr.com/fgfmpgh/7xukzalz8/jquery.easing.js"></script>
<script type="text/javascript">
$(function() {
$("#sidebarupcoming").scrollToTop({speed:500,start:700});
});
</script>
<!– / Scroll –>

With this script the 'show on scroll' function works, but when you click on the links the 'jump to name tag' won't work, instead, the page will jump to the top. 使用此脚本,“滚动显示”功能起作用,但是当您单击链接时,“跳转到名称标签”将不起作用,而是将页面跳转到顶部。 When I remove the script, jumping to the name tag works, but the sidebar isn't hidden anymore. 当我删除脚本时,跳转到名称标签即可,但是边栏不再隐藏。 How do I get this to work? 我该如何工作? (that this, sidebar 'showing on scroll' and jumping to the right name tag?) (侧边栏“正在滚动显示”并跳到右侧的名称标签?)

The scroll() method is an alias for on('scroll', handler) , an event handler that is triggered on scroll, $(window).scrollTop() returns the distance in pixels from the top. scroll()方法是on('scroll', handler)的别名, on('scroll', handler)是在滚动上触发的事件处理程序, $(window).scrollTop()返回距顶部的距离(以像素为单位)。 You made like to customise the hiding animation effect to your needs, I recommend .animate() , .hide() , or .fadeOut() 你让喜欢自定义隐藏动画效果您的需求,我建议.animate() .hide().fadeOut()

JavaScript JavaScript的

$(function() {
    $(window).scroll(function(e) {
        if ($(this).scrollTop() > 700)
        {
            $("#sidebarupcoming").fadeIn("slow");
        }
        else
        {
            $("#sidebarupcoming").hide(); 
        }
    });
});

And click on the anchors <a href='#week36'>WEEk 36 </a> should work as expected, if you wish to animate a scrolling motion to that element you should look at this SO Answer . 然后单击锚点<a href='#week36'>WEEk 36 </a>应该可以正常工作,如果您希望为该元素设置滚动动画,则应查看SO Answer

In that case I would recommend adding a class to each of these links such as animated-scroll like below: 在这种情况下,我建议向每个链接中添加一个类,例如如下所示的animated-scroll

JavaScript JavaScript的

$(".animated-scroll").click(function (e){
    var elementID = $(this).attr('href');
    e.preventDefault(); // stop default behaviour
    $('html, body').animate({
        scrollTop: $(elementID).offset().top
    }, 2000);
});

HTML HTML

<ul id="sidebarupcoming">
  <li><a href="#week36" class='animated-scroll'>WEEK 36 </a></li>
  <li><a href="#week37" class='animated-scroll'>WEEK 37 </a></li>
  <li><a href="#week38" class='animated-scroll'>WEEK 38 </a></li>
</ul>

Credits: 学分:

Check the jquery docs 检查jQuery文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM