在移动css布局的页面滚动上添加css规则

add css rules on page scroll with mobile css layout

本文关键字:css 滚动 添加 规则 移动 布局      更新时间:2023-09-26

在我的网站中,我想在页面顶部显示一个特定的div,只有当用户向下滚动时。当用户进入首页时,这个特殊的div必须消失。

如何激活滚动页上的css规则?

.particular-div{
.....
not show on top rules
....
}
.particular-div-on-scroll-down{
set the div on fixed top position;
top:0px; position :fixed;
}

我该怎么办?我必须使用javascript?谢谢

升级:这是我的代码,但它不起作用:

CSS

 @media only screen and (max-device-width: 480px) {
    .shares{
        top:-100px;left: 0px;
        position:fixed;
        background:#efe3af;
    }
    .sharesShow{
    top:0px;left: 0px;
    }
}

Javascript:

<script>
            $(document).ready(function () {
                $(window).scroll(function () {
                    if ($(window).scrollTop() + $(window).height() >     $(window).height() + 10) {
                        $("div.shares").addClass("sharesShow");
                    } else {
                        $("div.shares").removeClass("sharesShow");
                    }
                });
            });
        </script>

如果您能够使用Jquery,则可以很容易地跟踪scroll事件:

$(window).scroll(function(){
  if($(this).scrollTop() > 0){
    //Do stuff if the user scroll down in this case show the div
    $('.particular-div').addClass('show');
  } else {
    //Do stuff if the user scroll to the top in this case hide the div
    $('.particular-div').removeClass('show');
  }
})

检查此Demo Fiddle

<!DOCTYPE html>
<html>
<style>
body, html {
    margin:0;
    padding:0;
}
header {
    display:none;
    position: relative;
    width: 100%;
    height: 60px;
    line-height: 60px;
    background: #000;
    color: #fff;
}
header.animateIt {
    top:0;
    position:fixed;
    left: 0;
    right: 0;
    z-index:999;
}
.content {
    padding: 0 20px 20px;
    background: #fff;
    line-height: 1.5;
    color: #333;
}
</style>
<body>
<div id="outer-ring">
    <header id="head">
    Header Content goes here   
</header>
<!-- just to make scroll content is added -->
<div class="content">
<p>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
    content
    <br/>
</p>
</div>
</div>
</body>
<script src="jquery.js"></script>
<script>
$(document).ready(function () {
    $(window).on("scroll", function () {
       console.log($(window).scrollTop())
       if($(window).scrollTop() > 50){ // header height..
           console.log("show")
            $('#head').show().addClass('animateIt');
       }else{
           console.log("hide")
            $('#head').hide().removeClass('animateIt');
       }
    });
});
</script>
</html>

试试这个!!

是的,使用JavaScript,甚至更容易使用jQuery。

我在JSFiddle上举了一个例子:http://jsfiddle.net/46s3o8Lm/1/

$(window).scroll(function () {
    if ($(window).scrollTop() + $(window).height() >     $(window).height() + 10) {
        $("div.particular-div").addClass("particular-div-on-scroll-down");
    } else {
        $("div.particular-div").removeClass("particular-div-on-scroll-down");
    }
});

将数字10更改为用户必须向下滚动直到添加类的像素数。