jQuery:滚动到 - 更改div ID

时间:2012-06-25 19:46:27

标签: jquery css html scroll scrollto

我遇到了jQuery的问题,当滚动条处于20px时,我想更改DIV(不是DIV的内容,而是DIV NAME div id =" DIVNAME")。

在滚动条= 20px之前:

  

divname {   宽度:1012px;   保证金:25px auto 0 auto;   身高:50px;   }

滚动条+ 20px后:

  

divname2 {   宽度:1012px;   保证金:25px auto 0 auto;   身高:50px;   位置:已修复;   }

我的具体案例:

image http://i.minus.com/jjjWR2hKWhhFw.jpg

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

您可以这样做,例如向父div添加一个类以使其修复。将发布代码并尽快摆弄。

Yup完成代码:

的JavaScript

$(document).ready(function(){
    $(window).scroll(function(){
       if($(".check").offset().top < $(window).scrollTop())
           $(".check").addClass("fixed");
        else
           $(".check").removeClass("fixed"); 
    });
});​

CSS

​ul, ol, p, li {padding: 5px 0;}
h1 {font: 2em Tahoma bold; text-align: center;}
div.check.fixed h1 {position: fixed; top: 0; width: 100%; background: #fff;}

HTML

<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

<div class="check">
    <h1>To be fixed!</h1>
</div>

<ul>
  <li>Coffee</li>
  <li>Tea</li>
  <li>Milk</li>
  <li>Coffee</li>
  <li>Tea</li>
  <li>Milk</li>
</ul>

<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>

<ol>
  <li>Coffee</li>
  <li>Tea</li>
  <li>Milk</li>
  <li>Coffee</li>
  <li>Tea</li>
  <li>Milk</li>
</ol>

<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>

<p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p>​

小提琴

http://jsfiddle.net/ekeSL/

观看直播

更改div

的名称

不建议更改id的{​​{1}}。但是如果你坚持下去,你可以这样做:

div

答案 1 :(得分:1)

如果您要更改的是position,则可能更容易添加类或附加css属性。

if (scrollbar > 20px)
    $("#divname").css("position" : "fixed");