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

时间:2014-09-23 16:11:50

标签: javascript css mobile

在我的网站中,我想在页面顶部显示一个特定的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? 谢谢

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

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>

3 个答案:

答案 0 :(得分:2)

如果您能够使用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

答案 1 :(得分:1)

<!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>

试试这个!!

答案 2 :(得分:1)

是的,使用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,直到添加该类。