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

问题描述

【求助】如何用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>

解决方案

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

解决方案二:

http://www.jq22.com/jquery-info3300

这个。

下面这个跟你的需求不太符合,但是你也可以看下:

http://www.jq22.com/jquery-info3670

解决方案三:

既然你有网页,直接用IE F12功能去复制css和htm

解决方案四:

JS实现div自动滚动

解决方案五:

http://sc.chinaz.com/jiaoben/130508545900.htm

你看下这个可以解决你的问题吗?

看了下,相似度90%。

时间: 2024-12-02 20:14:11

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

JS当前页面登录注册框,固定DIV,底层阴影的实例代码_javascript技巧

这是一个实例,保存代码为html文件运行试试吧.兼容火狐.ie6.ie7.ie8.Chrome等. <!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"&

js判断页面中是否有指定控件的简单实例

本篇文章主要是对js判断页面中是否有指定控件的简单实例进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助 if (document.getElementById('show_pic')!=null){document.getElementById('show_pic').src=this.value;} show_pic是页面中的一个图片控件,我们首先要保证这个控件存在于页面上,才能对其进行操作,不然JS就会出现错误,所以用这种方式进行了判断,我听网友说,JS中还有一个函数,可以判断对象是

JS判断页面是否出现滚动条的方法_javascript技巧

本文实例讲述了JS判断页面是否出现滚动条的方法.分享给大家供大家参考.具体如下: var isScroll = function (el) { // test targets var elems = el ? [el] : [document.documentElement, document.body]; var scrollX = false, scrollY = false; for (var i = 0; i < elems.length; i++) { var o = elems[i]

js判断鼠标位置是否在某个div中的方法_javascript技巧

本文实例讲述了js判断鼠标位置是否在某个div中的方法.分享给大家供大家参考,具体如下: div的onmouseout事件让div消失时,会出现这样的情况,就是当鼠标移至div中的其它内容时,此时也判定为离开div,会触发 onmouseout事件,这样div中的内容就不能操作了.解决的办法是当触发onmouseout事件时,先判断鼠标是否在div内,如果在,说明鼠 标并没有离开div,就不删除div,否则,删除之.OK,现在问题解决了. 就是找到该div左上角和右下角坐标,判断鼠标的坐标是否在

用js判断页面是否加载完成实现代码_基础知识

用document.onreadystatechange的方法来监听状态改变, 然后用document.readyState == "complete"判断是否加载完成 代码如下: 复制代码 代码如下: document.onreadystatechange = subSomething;//当页面加载状态改变的时候执行这个方法. function subSomething() { if(document.readyState == "complete") //当页

JS判断页面加载状态以及添加遮罩和缓冲动画的代码_javascript技巧

复制代码 代码如下: function initialize() { addcloud(); //为页面添加遮罩 document.onreadystatechange = subSomething; //监听加载状态改变 } function addcloud() { var bodyWidth = document.documentElement.clientWidth; var bodyHeight = Math.max(document.documentElement.clientHei

js判断页面是关闭还是刷新

程序  代码如下 复制代码 window.onunload = function(){                var a_n = window.event.screenX - window.screenLeft;                var a_b = a_n > document.documentElement.scrollWidth-20;                if(a_b && window.event.clientY< 0 || window

用js判断页面刷新或关闭的方法(onbeforeunload与onunload事件)_javascript技巧

Onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过window.onunload来指定或者在<body>里指定.区别在于onbeforeunload在onunload之前执行,它还可以阻止onunload的执行. Onbeforeunload也是在页面刷新或关闭时调用,Onbeforeunload是正要去服务器读取新的页面时调用,此时还没开始读取:而onunload则已经从服务器上读到了需要加载的新的页面,在即将替换掉当前页面时调

js判断鼠标同时离开两个div的思路及代码_javascript技巧

纠结了两天,憋出了个蛋,欢迎拍砖!!! 复制代码 代码如下: <!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> <