2013-07-17 72 views
1

我希望此腳本在用戶滾動第二個div時運行。第二個div在「內容」div的意義上說。第一個div是標題。在滾動動畫腳本。在滾動動畫腳本。它只運行一次

<!doctype html> 
<head> 
<script type="text/javascript"> 
$(document).ready(function(){ 
    $('.content').scroll(function(){ 
     if ($(this).scrollTop() > 100) 
     { 
      $(".header").animate({"height": "300px"},"slow"); 
     } 
     else 
     { 
      $(".header").animate({"height": "100px"},"fast"); 
     } 
    }); 
}); 
</script> 
<style> 
body{ 
margin:auto; 
overflow:hidden; 
background-color:#000; 
} 
.outer{ 
width:800px; 
border:thin solid; 
height:900px; 
background-color:#fff; 
margin:auto; 
} 
.wrapper{ 
width:800px; 
position:relative; 
z-index:100; 
height:900px; 
} 
.header{ 
width:800px; 
height:300px; 
border: thin solid #F00; 
background-color:#666; 
} 
.content{ 
width:800px; 
height:600px; 
overflow:scroll; 
} 
</style> 
</head> 
<body> 
<div class="outer"> 
<div class="wrapper"> 
<div class="header"></div> 
<div class="content"> 
<p>Dummy content so that the content makes the div to scroll.</p> 
</div> 
</div> 
</div> 
</body> 
</html> 

只要用戶滾動div,腳本就必須運行。

+1

你可以提供一個[的jsfiddle(http://jsfiddle.net/)嗎? – MisterJ

+2

或HTML與上述 – Pete

+0

嗨皮特我已經添加了HTML和CSS代碼。感謝你的迴應。 – Ravi

回答

0

這是工作在谷歌瀏覽器:http://jsfiddle.net/3kySD/2/ 我不知道爲什麼,但在我的Firefox(v22.0)做它是工作,但非常緩慢。 我已經改變了只在代碼中的小東西,導致你認爲這是不是在我看來,工作...你有這個測試:

if ($(this).scrollTop() > 100) 

我這一個替代它的,所以標題欄將是最大的當內容DIV是在頂部,當你正在閱讀的內容的div將會更小:

if ($(this).scrollTop() < 100) 
+0

Hello MisterJ,謝謝你的解答。正如你所說,它在Firefox中速度非常慢 - 這導致我認爲它不工作。謝謝你的幫助。 – Ravi

+0

如果你認爲它關閉了你的請求,你應該點擊我答案左邊的複選框。否則可以自由地告訴你想要什麼,但是一個新問題可能更合適。 – MisterJ

0

添加.stop()

$(".header").stop().animate({"height": "300px"},"slow"); 
+0

嗨Sbml,我希望腳本在用戶滾動內容div時運行。感謝你的迴應。添加.stop()後,它阻止腳本從一次運行。 – Ravi