jQuery實現滑鼠經過購物車出現下拉框代碼(推薦),jquery下拉框

來源:互聯網
上載者:User

jQuery實現滑鼠經過購物車出現下拉框代碼(推薦),jquery下拉框

這一段時間在學習web前端,最近學了jQuery庫,深感其強大,下面通過寫購物車的下拉框做法,把自己的理解和大家交流一下,歡迎各位大神指點指正,廢話不多說,開始正題:

  購物車html:

<!-- 購物車 start --><div class="shopping" id="shopping-box"><a href="" id="shoptext"><i class="iconfont"></i> 購物車(0)</a><!-- 購物車下拉框 start--><div class="shop" id="shop-content">購物車中還沒有商品,趕緊選購吧!</div><!-- 購物車下拉框 end--></div><!-- 購物車 end -->

  剛開始學習原生js時候的寫法:

//購物車下拉框 startvar shoppingBoxNode = document.getElementById("shopping-box");var shopContentNode = document.getElementById("shop-content");var shoptext = document.getElementById("shoptext");shoppingBoxNode.onmouseenter = function(){shoptext.style.background = "#fff";shoptext.style.color = "#ff6700";shopContentNode.style.display = "block";console.log("over");};shoppingBoxNode.onmouseleave = function(){shoptext.style.background = "";shoptext.style.color = "";shopContentNode.style.display = "";console.log("out");};//購物車下拉框 end

  感覺很麻煩,而且還不好理解,下面用jQuery來寫的:

//購物車 下拉var interval1;$("#shopping-box").mouseenter(function(){clearTimeout(interval1);$(this).children().first().css({"color":"#ff6700","background":"#fff"});$(this).children().last().stop(true,true).slideDown();}).mouseleave(function(){var self = $(this);interval1 = setTimeout(function(){self.children().first().removeAttr("style");},700);$(this).children().last().delay(200).slideUp();});

  這個看著就乾淨利落的多,相對的減少了代碼量,這裡面事件使用應用鏈的寫法,而且jQuery的方法的相容問題基本上在其內被都已經被解決了,這點真是讓前端的工作量減少了很多,用原生的時候調相容調的頭都快炸了(大家都懂的。。。),裡面用到了jQuery中的延時delay和停止動畫stop來處理(很好用的兩個函數),當滑鼠移動過快出現的問題

  這裡面事件的寫法當然也可以用下面的方法(on也可以用bind來替換):

//購物車 下拉var interval1;$("#shopping-box").on({mouseenter:function(){},mouseleave:function(){ }});

以上所述是小編給大家介紹的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.