CSS Sprite (JS)

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   sp   on   

HTML

 1 <body> 2     <!-- ul.sprite>li*5>s.s-icon+a{CSS Sprite} --> 3     <!-- 以上是Sublime Text快速拼字 --> 4     <ul class="sprite"> 5         <li><s class="s-icon"></s><a href="">CSS Sprite</a></li> 6         <li><s class="s-icon"></s><a href="">CSS Sprite</a></li> 7         <li><s class="s-icon"></s><a href="">CSS Sprite</a></li> 8         <li><s class="s-icon"></s><a href="">CSS Sprite</a></li> 9         <li><s class="s-icon"></s><a href="">CSS Sprite</a></li>10         <div class="clear"></div>11     </ul>12 </body>

CSS

 1 <style> 2     body { background-color: #fc0; color:#333;} 3     * {margin:0; padding:0;} 4     ul,li {list-style: none;} 5     a { color:#f00; font-weight: bold; text-decoration: none;} 6     .sprite {margin:0 auto; width:1000px; padding-top: 50px;} 7     .sprite li {float:left; margin-left: 50px; text-align: center; cursor:pointer; } 8     .sprite li s { display: block; width:132px; height:112px; background:url(all.png) no-repeat; } 9     .clear { clear:both;}10 </style>

JS

 1 <script src="http://code.jquery.com/jquery-latest.js"></script> 2     <script> 3     $(function () { 4         var iconH = $(".sprite").find("s").height(), 5             //找出存放圖片的容器的高度; 6             triggerLi = $(".sprite").children("li"); 7             //找出每一個li,放到一個數組中; 8         //console.log(iconH); 9         //在控制台列印出容器的高度;10         triggerLi.each(function () {11         //遍曆數組中的每一個li12             var $this = $(this),13                 //聲明變數賦值當前的li;14                 $index = $this.index();15                 //聲明變數儲存當前li的index值;16             //console.log($index);17             //在控制台列印出每一個li的index值;18             //console.log(iconH*$index);19             //得出每一個圖片對應的position值;20             $this.children("s").css("background-position","0 -"+iconH*$index+"px");21             //利用js遍曆出每一個s標籤的背景圖片;22             $this.hover(function() {23                 //滑鼠移入24                 $this.children("s").css("background-position","-132px -"+iconH*$index+"px");25             }, function() {26                 //滑鼠移出27                 $this.children("s").css("background-position","0 -"+iconH*$index+"px");28             });29         })30     })31     </script>

IMG(右鍵儲存即可,重新命名all.png)

SHOW

 

CSS Sprite (JS)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.