DIV CSS 佈局教程網

jQuery 背景圖片滑動效果:animate 動到 background-position
編輯:JQuery常見問題     

先看效果:https:///keleyi/phtml/jquery/7.htm

做法是對 backgroundPosition 屬性下 animate,讓背景圖沿水平方向移動,看起來就像圖片在容器裡滑動。

CSS

容器要有明確的寬高,背景設定 no-repeat,並把起始位置放在 left center:

.animate{
  height:300px;
  width:600px;
  background:url(https:///image/a/tvh00n12.jpg) no-repeat;
  background-position:left center;
}

JavaScript

$(function () {
  $('#start_keleyi_com').click(function () {
    $("div#animate_ke"+"leyi_com").animate({ backgroundPosition: "400" }, "slow");
  });
  $('#back_keleyi_com').click(function () {
    $("div#animate_ke" + "leyi_com").animate({ backgroundPosition: "0" }, "slow");
  });
});

HTML

<div id="animate_keleyi_com" class="animate">背景圖片動起來

  <ul>
    <li><a href="https:///a/bjac/768f469b95b61487.htm">單行文字間歇向上滾動</a></li>
    <li><a href="https:///a/bjac/a6d651710217f7a0.htm">jQuery UI修飾title屬性的氣泡懸浮框</a></li>
    <li><a href="https:///a/bjac/bf0eb8c02085b10d.htm">jquery清空textarea等輸入框</a></li>
    <li><a href="https:///a/bjac/939631bb07adb4dc.htm">jquery關燈特效</a></li>
  </ul>
</div>
<a id="back_keleyi_com" href="javascript:void(0)"><-動起來吧!</a>
<a id="start_keleyi_com" href="javascript:void(0)">動起來吧!-></a>

要留意的細節

  • 數值單位:這裡只給了數字,jQuery 會當作像素處理。若需要百分比定位,得自行用 step 或改用 CSS 動畫。
  • 瀏覽器差異:background-position 是複合屬性,早期 Firefox/Chrome 對動畫解析的格式不完全一致,比較穩定的寫法是拆成 backgroundPositionX 與 backgroundPositionY,或直接動到足夠大的單值。
  • 平滑循環:想做無縫捲動,通常會把背景重複(repeat-x)並把終點設為背景寬度的倍數,動完再瞬間歸零。
  • 動畫前 stop():連續點擊時加上 .stop() 可避免動畫堆疊。

更多參考:https:///a/bjac/5b62295315de9ace.htm

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved