jquery 超簡單的點贊效果

來源:互聯網
上載者:User

標籤:blog   http   ar   io   color   os   sp   for   java   

1.HTML(可以最佳化一下,盡量少些幾個標籤.....)

                <div id="dianz">                    <b class="cz"><em>1</em><i></i><s></s><u>超贊</u></b>                    <b class="tj"><em>2</em><i></i><s></s><u>推薦</u></b>                    <b class="yb"><em>3</em><i></i><s></s><u>一般</u></b>                    <b class="wl"><em>6</em><i></i><s></s><u>無聊</u></b>                    <b class="lj"><em>5</em><i></i><s></s><u>雷囧</u></b>                </div>

2.css樣式

#dianz{text-align:center; width:610px; overflow:hidden;zoom:1; margin:20px auto;}#dianz b{ display:inline-block; width:120px; height:215px; float:left; position:relative;}#dianz b em,#dianz b u,#dianz b i,#dianz b s{display:inline-block; width:100%; height:20px; position:absolute; left:0px;}#dianz b u{ bottom:0px;}#dianz b s{ bottom:20px; height:95px;}#dianz b i{width:20px; height:80px;left:50px; bottom:115px;}#dianz b.cz s{ background:url(../images/dianz.jpg) 25px 0px no-repeat}#dianz b.cz i{ background-color:#fe0032;}#dianz b.tj s{ background:url(../images/dianz.jpg) -105px 0px no-repeat}#dianz b.tj i{ background-color:#fe9903;}#dianz b.yb s{ background:url(../images/dianz.jpg) -235px 0px no-repeat}#dianz b.yb i{ background-color:#99c900;}#dianz b.wl s{ background:url(../images/dianz.jpg) -370px 0px no-repeat}#dianz b.wl i{ background-color:#32ccff;}#dianz b.lj s{ background:url(../images/dianz.jpg) -500px 0px no-repeat}#dianz b.lj i{ background-color:#3167ff;}

3.js(對js運用的不是非常好,大家可以最佳化的更好一些)

   function o_dianz(){   var oi=$(‘#dianz b i‘); //擷取i;       oem=$(‘#dianz b em‘); //擷取em;   os=$(‘#dianz b s‘);//擷取s;   bl=null;   osz=null;   Arr=[];   Arr2=[];   function o_mm(){   oem.each(function(){  osz=$(this).text();  Arr.push(osz);  //console.log(Arr)});   var get_max=Math.max.apply(null,Arr); //擷取最大點贊數;   bl=80/get_max;        oem.each(function(){  osz=$(this).text();  var oi_H=Math.floor(osz*bl);  Arr2.push(oi_H);});   for(var i=0; i<Arr2.length; i++){      oi.eq(i).height(Arr2[i]);  oem.eq(i).css(‘top‘,80-Arr2[i]);   };    };o_mm();    os.click(function(){  //點贊增加;     Arr=[];      Arr2=[];  osz=$(this).siblings(‘em‘).text();  osz++;  $(this).siblings(‘em‘).text(osz);  o_mm();   });        };   o_dianz();

好了,代碼都貼上來了,超級簡單的。我寫的原理(不知道是不是有更好的,同時bug也沒有檢測):設定i標籤的預設高度為80px,然後通過js求出每一個em的text數值,丟入數組Arr中,再通過這個方法Math.max.apply(null,Arr),求最大text的數值,進而求出比例尺(通過最大text求出比例尺可以保證高度不會大於80PX),最後通過每一個text的數值乘以比例尺Math.floor(osz*bl),求出每一個em對應的高度值。

後面的點擊事件中每點擊一次表徵圖,相對應的重設一次Arr和Arr2,可以保證數值是即時更新的。。。 OK,到這裡就完了,,,  

最終結果:


 

jquery 超簡單的點贊效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.