Carol-Li 2015-09-04 10:14 采纳率: 75%
浏览 2449
已采纳

【求助】如何用JS判断页面滚动到特定的div,从而实现背景的更换

像这个网页http://mailchimp.com/2012/,当滚动条到下一篇文章的时候背景就更换了。
我自己的想法是——每个文章块给予一个id号,当这一文章块过了窗口上方的时候开始更换背景。

我不知道百度什么关键词比较能找到这方式,所以来这里请教各位大大。

我找到了一个貌似可以实现的代码:click me,以下是我改过后的代码,是没滚动400px就改变背景图。但是我想实现的不是滚动某个高度改变背景图,而是**到某个div的时候执行代码**。

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
body{
    background:#666;
    background-attachment: fixed;
    -webkit-transition: background 0.5s ease-out;
    -moz-transition: background 0.5s ease-out;
    -o-transition: background 0.5s ease-out;
}
.block{
    width:30%;
    /*position:absolute;*/
    margin:10% auto;
    position:fixed;
    font-family:Verdana, Geneva, sans-serif;
    text-align:center;
}
.block2{
    width:70%;
    /*position:relative;*/
    margin-left:auto;
    font-family:Arial, Helvetica, sans-serif;
    font-size:50px;
}
#title{
    color:#fff;
}
h2{
    color:#fff;
}
p{
    color:#FF9;
}
.block2>p{
    margin-top:200px;
}
</style>
<script type="application/javascript">
window.onload=function(){
    var changeBG=true,scrollTop=0;
    var title=document.getElementById("title");
    var phrase=document.getElementById("phrase");
    var changed_color="#222";
    var lastTop=0;
    //设置原始变量
    setInterval(function(){
        Top=document.body.scrollTop;
        if(Top>400+lastTop||Top<lastTop-400){
            if(changed_color=="#222"){
                document.body.style.backgroundColor=changed_color;
                title.innerHTML="World";
                phrase.innerHTML="hey, I changed!";
                changed_color="#666";
            }else{
                document.body.style.backgroundColor=changed_color;
                title.innerHTML="Hello";
                phrase.innerHTML="welcome to my homepage!";
                changed_color="#222";
            }
            lastTop=Top;
        }
    },1000)
}
</script>
</head>

<body>
<div class="block">
<h1 id="title">Hello</h1>
<p id="phrase">welcome to my homepage!</p>
</div>
<div class="block2">
<div id="a">
<h2>1. Keep good friends around</h2>
<p>Keeping good company will keep you socially happy and healthy throughout your life. On the other hand, nothing will drag you down more than socializing with people who hold you back from your full potential. Ironically, surrounding yourself with people who are intent on moving forward in life will keep you energized and make you feel as if you’re always in your prime. It’s when you find yourself surrounded by toxic friends who live in the past that you realize you’re not getting any younger, and you’ll start to feel as if the best days of your life have passed you by.
</p>
</div>
<div id="b">
<h2>2. Continue learning</h2>
<p>Saying someone is “old-fashioned” is just a nice way of saying their behind the times. If you get into the mindset that you’re “too old” to learn something (like how to use Windows 10 or an iPhone), you’ll certainly feel that you’re past your prime. We live in a world in which education and knowledge is literally at our fingertips, and it’s never been easier to pick up a new skill or hobby. Make it a habit to learn something new every day, and you’ll continue feeling sharp and ready to take on the world. Soon enough, you’ll be showing your children tips and tricks on the iPhone 6!
 </p>
</div>
<div id="c">
<h2>3. Enjoy the little things</h2>
<p>When we were kids, we jumped in puddles, rolled down hills, and ate snowflakes falling from the sky. I’m not saying you, as a grown adult, should do these things habitually (except eating snowflakes; you’re never too old for that). But you should never overlook the small things in life. Celebrate your good hair days. Feel victorious when you hit every green light on the way home from work. Actively look for things to enjoy in your life; no matter how much may be going wrong, there will always be something to smile about if you look hard enough.
 </p>
</div>
</div>
</body>
</html>

  • 写回答

4条回答 默认 最新

  • 微wx笑 Java领域优质创作者 2015-09-05 00:57
    关注

    jQuery 判断某个元素是否可见
    $(element).is(":visible");

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止