標籤:osi abs linear code 改版 script top absolute log
---恢複內容開始---
由於對JQuery不熟悉,折騰了半天......
但不管怎麼說,總算是完成了,也是一點進步。
先上完成後的代碼。
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> .section{ position:relative; font-size: 14px; } .clearfix::after{ content: ""; display:block; height: 0; clear: both; } .tab{ position: relative; margin: 0; padding: 0; text-align: center; } .singer{ float: left; width: 100px; height: 30px; line-height: 30px; border: 1px solid #ccc; margin-left: -1px; /*重疊邊距,美觀*/ background: linear-gradient(#fefefe, #ededed); list-style: none; cursor: pointer; } .on{ border-bottom:0 ; background:#fff } .songs{ position: absolute; width:403px; height: 300px; margin-top: -1px; /*重疊邊距,美觀*/ margin-left: -1px; border: 1px solid #ccc; font-size:12px; text-indent: 14px; opacity: 0; } .show{ opacity: 1; border-top:#fff; } </style></head><body> <section> <ul class="tab clearfix" id="tab"> <li class="singer on">周杰倫</li> <li class="singer">李健</li> <li class="singer">薛之謙</li> <li class="singer">林俊傑</li> </ul> <div class="songs show"> <p>退後、本草綱目、菊花台、青花瓷</p> </div> <div class="songs"> <p>假如愛有天意、我願人長久、童年、春風十裡不如你</p> </div> <div class="songs"> <p>醜八怪、紳士、演員、方圓幾裡</p> </div> <div class="songs"> <p>江南、殺手、醉赤壁、一千年以後</p> </div> </section></body></html>
上面是HTML/CSS代碼,沒什麼大問題。
接下來轉場效果,我們用jQuery實現,這個過程夠折騰,一把辛酸淚。
用jQuery的時候一定不要和原生js混用!用jQuery的時候一定不要和原生js混用!用jQuery的時候一定不要和原生js混用!重要的事情說三遍!
不然,就是下面這種效果....
<script> //思路混雜,原生js和jQuery混用,丟死人了... $(document).ready(function(){ var singers=$(".singer"); var songs=$(".songs"); var index=0; var len=singers.length; for(var i=0;i<len;i++){ singers[i].onmouseover=function(){ singers.eq(index).removeClass("on"); songs.eq(index).removeClass("show"); singers.eq(i).addClass("on"); songs.eq(i).addClass("show"); index=i; } } }) </script>
所以啟動並執行時候那畫面太美我不敢看,程式員有三寶:一看、二查、三請教。崩潰的我於是跑去找大神了,大神一掃代碼,三下五除二給了我幾行代碼,效果搞定!
下面是大神的代碼修改版。
<script src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script> <script> $(function(){ // 先選擇好所有的 菜單li 和 面板div var li = $(‘li.singer‘), song = $(‘div.songs‘); // mouse指標位菜單li上方時的事件 li.mouseover(function(){ // 當前是第幾個li元素 var n = $(this).index(); // 先讓菜單和面板恢複成預設 li.removeClass("on"); song.removeClass("show"); // 再分別加上啟用的效果 li.eq(n).addClass("on"); song.eq(n).addClass("show"); }); }); </script>
大神就是大神,佩服得五體投地。小白仰望星星眼~
當然,我不想一字不差照抄大神的代碼,於是我在在這個基礎上繼續改善完成了My Code,再添加一個淡入效果。
於是最終完成版是這樣子滴。
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> .section{ position:relative; font-size: 14px; } .clearfix::after{ content: ""; display:block; height: 0; clear: both; } .tab{ position: relative; margin: 0; padding: 0; text-align: center; } .singer{ float: left; width: 100px; height: 30px; line-height: 30px; border: 1px solid #ccc; margin-left: -1px; /*重疊邊距,美觀*/ background: linear-gradient(#fefefe, #ededed); list-style: none; cursor: pointer; } .on{ border-bottom:0 ; background:#fff } .songs{ position: absolute; width:403px; height: 300px; margin-top: -1px; /*重疊邊距,美觀*/ margin-left: -1px; border: 1px solid #ccc; font-size:12px; text-indent: 14px; opacity: 0; } .show{ opacity: 1; border-top:#fff; } </style></head><body> <section> <ul class="tab clearfix" id="tab"> <li class="singer on">周杰倫</li> <li class="singer">李健</li> <li class="singer">薛之謙</li> <li class="singer">林俊傑</li> </ul> <div class="songs show"> <p>退後、本草綱目、菊花台、青花瓷</p> </div> <div class="songs"> <p>假如愛有天意、我願人長久、童年、春風十裡不如你</p> </div> <div class="songs"> <p>醜八怪、紳士、演員、方圓幾裡</p> </div> <div class="songs"> <p>江南、殺手、醉赤壁、一千年以後</p> </div> </section> <script src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script> <script> $(function(){ var singers=$(".singer"); var songs=$(".songs"); var p=$("p"); singers.mouseover(function(){ var n=$(this).index(); singers.removeClass("on"); songs.removeClass("show"); p.hide(); singers.eq(n).addClass("on"); songs.eq(n).addClass("show"); p.eq(n).fadeIn(); }) }) </script></body></html>
每天進步一點點。
---恢複內容結束---
tab 選項卡(初用jQuery)