jQuery使用hide()、toggle()函數(shù)實(shí)現(xiàn)相機(jī)品牌展示隱藏功能
最近在學(xué)習(xí)jQuery時(shí)接觸到了show()、hide()、toggle()函數(shù),于是利用這幾個(gè)函數(shù)練習(xí)了一個(gè)使元素顯示隱藏的案例:
小提示:代碼中切換按鈕上下的小圖標(biāo)可以在此鏈接品牌展示功能圖片中獲取
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>使用hide()、toggle()函數(shù)實(shí)現(xiàn)相機(jī)品牌展示</title> <style type="text/css"> * { margin: 0; padding: 0; } body { font-size: 12px; text-align: center; } a { color: #04D; text-decoration: none; } a:hover { color: #F50; /*text-decoration 屬性規(guī)定添加到文本的修飾,下劃線、上劃線、刪除線等。*/ text-decoration: underline; } .SubCategoryBox { width: 600px; margin: 0 auto; text-align: center; margin-top: 40px; } .SubCategoryBox ul { list-style: none; } .SubCategoryBox ul li { display: block; float: left; width: 200px; line-height: 20px; } .showmore, .showless { clear: both; text-align: center; padding-top: 10px; } .showmore a, .showless a { display: block; width: 120px; margin: 0 auto; line-height: 24px; border: 1px solid #AAA; } .showmore a span { padding-left: 15px; /*最后兩位數(shù)字是以左上角為(0,0)的坐標(biāo)做一個(gè)偏移 第一個(gè)數(shù)字是X軸上的偏移量,也就是橫向的偏移量,正表示向右,負(fù)表示向左! 第二個(gè)數(shù)字是Y軸上的偏移量,也就是橫向的偏移量,正表示向下,負(fù)表示向上!*/; background: url(img/down.gif) no-repeat 0 3px; } .showless a span { padding-left: 15px; background: url(img/up.gif) no-repeat 0 3px; } .promoted a { color: #F50; } </style> <script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript"> $(function () { // 頁面加載完成先隱藏部分相機(jī)品牌 $("ul li:gt(5):not(:last)").hide(); // filter函數(shù)篩選出與指定表達(dá)式匹配的元素集合。這個(gè)方法用于縮小匹配的范圍。用逗號(hào)分隔多個(gè)表達(dá)式 // 這里篩選出保留需要單獨(dú)添加樣式的相機(jī)品牌 var multiPromoted = $("li").filter(":contains('佳能'),:contains('索尼'),:contains('柯達(dá)')"); // 獲取到a標(biāo)簽綁定點(diǎn)擊事件 $("div div a").click(function () { // 切換顯示與隱藏部分相機(jī)品牌 $("ul li:gt(5):not(:last)").toggle(); // 隱藏部分相機(jī)品牌時(shí)替換文字內(nèi)容、角標(biāo)圖片、移除li下a標(biāo)簽文字樣式 if ($("ul li:gt(5):not(:last)").is(":hidden")) { $("a > span").html("顯示全部品牌"); $("div div").removeClass(); $("div div").addClass("showmore"); $(multiPromoted).removeClass("promoted"); } else { // 顯示部分相機(jī)品牌時(shí)替換文字內(nèi)容、角標(biāo)圖片、添加li下a標(biāo)簽文字樣式 $("a > span").html("顯示精簡品牌"); $("div div").removeClass(); $("div div").addClass("showless"); $(multiPromoted).addClass("promoted"); } }); }); </script> </head> <body> <div class="SubCategoryBox"> <ul> <li><a href="#">佳能</a><i>(30440) </i></li> <li><a href="#">索尼</a><i>(27220) </i></li> <li><a href="#">三星</a><i>(20808) </i></li> <li><a href="#">尼康</a><i>(17821) </i></li> <li><a href="#">松下</a><i>(12289) </i></li> <li><a href="#">卡西歐</a><i>(8242) </i></li> <li><a href="#">富士</a><i>(14894) </i></li> <li><a href="#">柯達(dá)</a><i>(9520) </i></li> <li><a href="#">賓得</a><i>(2195) </i></li> <li><a href="#">理光</a><i>(4114) </i></li> <li><a href="#">奧林巴斯</a><i>(12205) </i></li> <li><a href="#">明基</a><i>(1466) </i></li> <li><a href="#">愛國者</a><i>(3091) </i></li> <li><a href="#">其它品牌相機(jī)</a><i>(7275) </i></li> </ul> <div class="showmore"> <a href="#"><span>顯示全部品牌</span></a> </div> </div> </body> </html>
代碼運(yùn)行效果:
功能展示效果:
到此這篇關(guān)于jQuery使用hide()、toggle()函數(shù)實(shí)現(xiàn)相機(jī)品牌展示隱藏功能的文章就介紹到這了,更多相關(guān)jQuery使用hide()、toggle()函數(shù)實(shí)現(xiàn)相機(jī)品牌展示隱藏功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS實(shí)現(xiàn)點(diǎn)擊生成UUID的方法完整實(shí)例【基于jQuery】
這篇文章主要介紹了JS實(shí)現(xiàn)點(diǎn)擊生成UUID的方法,結(jié)合完整實(shí)例形式分析了基于jQuery實(shí)現(xiàn)的隨機(jī)字符串生成相關(guān)操作技巧,需要的朋友可以參考下2019-06-06jQuery仿天貓實(shí)現(xiàn)超炫的加入購物車
jquery仿照天貓購物車收藏商品特效,非常實(shí)用的一款商品加入購物車特效,動(dòng)畫效果非常炫麗。2015-05-05jackson解析json字符串,首字母大寫會(huì)自動(dòng)轉(zhuǎn)為小寫的方法
下面小編就為大家分享一篇jackson解析json字符串,首字母大寫會(huì)自動(dòng)轉(zhuǎn)為小寫的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12JQuery的自定義事件代碼,觸發(fā),綁定簡單實(shí)例
下面的代碼是所有它自己的自定義事件觸發(fā)。你可以綁定到這個(gè)自定義事件,并提高它時(shí),你要執(zhí)行的代碼里面綁定。2013-08-08jQuery獲取所有父級(jí)元素及同級(jí)元素及子元素的方法(推薦)
這篇文章主要介紹了jQuery獲取所有父級(jí)元素及同級(jí)元素及子元素的方法,本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值 ,需要的朋友可以參考下2018-01-01