如何在向下滚动时实现浮动div [英] How to achieve floating div on scrolldown

查看:295
本文介绍了如何在向下滚动时实现浮动div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是jquery的新手,并且当页面向下滚动超过50个时,我希望div挂在屏幕顶部吗?

I am a newbie on jquery, and I want the div to hang on top of the screen when the page scrolldown is more than 50, how can I achieve this?

我希望div始终是绝对的而不是固定的.

http://jsfiddle.net/8UCcY/

http://jsfiddle.net/8UCcY/

$(document).ready(function () {
    $(window).scroll(function () {
          if ($(window).scrollTop() > 50) {
              $(".articlebutton").css("top", "0px"); //I want this value to change dynamically as the scrollbar moves down, so that the div stays on top of screen
          } else {
              $(".articlebutton").css("top", "-50px");
          }
     });
});

推荐答案

您可以将其设置为顶部-100,因为它是-50,并且在50之后滚动:

You can set it to position top -100 since it is -50 and scroll occurs after 50:

$(".articlebutton").css("top", ($(window).scrollTop()-100)+"px");

这篇关于如何在向下滚动时实现浮动div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆