1

我对 jQuery 有疑问,我想在滚动条为 20px 时更改一个 DIV(不是 DIV 的内容,而是 DIV NAME div id="DIVNAME")。

在滚动条 = 20px 之前:

divname { 宽度:1012px; 边距:25px 自动 0 自动;高度:50px;}

滚动条 + 20px 之后:

divname2 { 宽度:1012px; 边距:25px 自动 0 自动;高度:50px; 位置:固定; }

我的具体情况:

图片 http://i.minus.com/jjjWR2hKWhhFw.jpg

谢谢您的帮助。

4

2 回答 2

2

您可以这样做,例如向父级添加一个类以div使其固定。将很快发布代码和小提琴。

是的,完成了代码:

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. div但是如果你坚持,你可以这样做:

$("div").attr("id", "newId");
于 2012-06-25T19:48:26.503 回答
1

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

if (scrollbar > 20px)
    $("#divname").css("position" : "fixed");
于 2012-06-25T19:52:22.543 回答