js+css實(shí)現(xiàn)換膚效果
本文實(shí)例為大家分享了js+css實(shí)現(xiàn)換膚效果的具體代碼,供大家參考,具體內(nèi)容如下
效果圖如下:
需求:點(diǎn)擊對(duì)應(yīng)小圓點(diǎn),下面內(nèi)容顏色跟著改變
主要思路:
1.在css中把對(duì)應(yīng)的樣式先寫好;
2.獲取小圓點(diǎn)給它綁定點(diǎn)擊事件;
3.獲取當(dāng)前點(diǎn)擊元素的類名;
4.將該類名設(shè)置給body;
js主要考察的是獲取屬性值和設(shè)置屬性值;
<style> ? ? ? ? *{ ? ? ? ? ? ? margin:0; ? ? ? ? ? ? padding:0; ? ? ? ? ? ? list-style: none; ? ? ? ? } ? ? ? ? .dot{ ? ? ? ? ? ?? ? ? ? ? ? ? margin:100px auto; ? ? ? ? ? ? display: flex; ? ? ? ? ? ? justify-content: center; ? ? ? ? } ? ? ? ? .dot li{ ? ? ? ? ? ? width: 30px; ? ? ? ? ? ? height: 30px; ? ? ? ? ? ? border-radius: 50%; ? ? ? ? ? ?cursor: pointer; ? ? ? ? } ? ? ? ? .dot li:first-child{ ? ? ? ? ? ? background:pink; ? ? ? ? } ? ? ? ? .dot li:nth-child(2){ ? ? ? ? ? ? background:green; ? ? ? ? } ? ? ? ? .dot li:nth-child(3){ ? ? ? ? ? ? background:gold; ? ? ? ? } ? ? ? ? .dot li:last-child{ ? ? ? ? ? ? background:skyblue; ? ? ? ? } ? ? ? ? .dot li:not(:last-child){ ? ? ? ? ? ? margin-right: 10px; ? ? ? ? } ? ? ? ? .content{ ? ? ? ? ? ? margin:100px auto; ? ? ? ? ? ? width: 300px; ? ? ? ? } ? ? ? ? .pink .content .banner{ ? ? ? ? ? ? height: 160px; ? ? ? ? ? ? width: 100%; ? ? ? ? ? ? background:pink; ? ? ? ? } ? ? ? ? .pink .content li{ ? ? ? ? ? ? color:pink; ? ? ? ? ? ? margin-bottom: 10px; ? ? ? ? ? ? border-bottom: 1px dashed pink; ? ? ? ? ? ? line-height: 40px; ? ? ? ? } ? ? ? ? .green .content .banner{ ? ? ? ? ? ? height: 160px; ? ? ? ? ? ? width: 100%; ? ? ? ? ? ? background:green; ? ? ? ? } ? ? ? ? .green .content li{ ? ? ? ? ? ? color:green; ? ? ? ? ? ? margin-bottom: 10px; ? ? ? ? ? ? border-bottom: 1px dashed green; ? ? ? ? ? ? line-height: 40px; ? ? ? ? } ? ? ? ? .gold .content .banner{ ? ? ? ? ? ? height: 160px; ? ? ? ? ? ? width: 100%; ? ? ? ? ? ? background:gold; ? ? ? ? } ? ? ? ? .gold .content li{ ? ? ? ? ? ? color:gold; ? ? ? ? ? ? margin-bottom: 10px; ? ? ? ? ? ? border-bottom: 1px dashed gold; ? ? ? ? ? ? line-height: 40px; ? ? ? ? } ? ? ? ? .skyblue .content .banner{ ? ? ? ? ? ? height: 160px; ? ? ? ? ? ? width: 100%; ? ? ? ? ? ? background:skyblue; ? ? ? ? } ? ? ? ? .skyblue .content li{ ? ? ? ? ? ? color:pink; ? ? ? ? ? ? margin-bottom: 10px; ? ? ? ? ? ? border-bottom: 1px dashed skyblue; ? ? ? ? ? ? line-height: 40px; ? ? ? ? } </style>
<body class="pink"> ? ? <ul class="dot"> ? ? ? ? <li class="pink"></li> ? ? ? ? <li class="green"></li> ? ? ? ? <li class="gold"></li> ? ? ? ? <li class="skyblue"></li> ? ? </ul> ? ? <div class="content"> ? ? ? ? <div class="banner"></div> ? ? ? ? <ul> ? ? ? ? ? ? <li>奶茶</li> ? ? ? ? ? ? <li>火鍋</li> ? ? ? ? ? ? <li>串串</li> ? ? ? ? ? ? <li>烤肉</li> ? ? ? ? </ul> ? ? </div> ? ? <script> ? ? ? ? window.onload = function(){ ? ? ? ? ? ? let lis = document.querySelectorAll('.dot li'); ? ? ? ? ? ? let body = document.querySelector('body'); ? ? ? ? ? ? for(let i=0;i<lis.length;i++){ ? ? ? ? ? ? ? ? lis[i].addEventListener('click',function(){ ? ? ? ? ? ? ? ? ? ? // 獲取屬性值:元素名.屬性名 ?設(shè)置屬性值:元素名.屬性名 = 屬性值 ; ?移除屬性:元素名.屬性名 = "";(此種方法不能獲取,設(shè)置,移除自定義屬性) ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? //獲取屬性值 :元素名.getAttribute('屬性名') ?;設(shè)置屬性值:元素名.setAttribute('屬性名','屬性值') ;移除屬性:元素名.removeAttribute('屬性名') (此種方法能獲取,設(shè)置,移除自定義屬性,可對(duì)任何屬性有效) ? ? ? ? ? ? ? ? ? ? let color = this.getAttribute('class') ? ? ? ? ? ? ? ? ? ? body.setAttribute('class',color) ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? } ? ? ? ? } ? ? </script> </body>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Bootstrap模態(tài)框(modal)垂直居中的實(shí)例代碼
這篇文章主要介紹了Bootstrap模態(tài)框(modal)垂直居中的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08Webpack中l(wèi)oader打包各種文件的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Webpack中l(wèi)oader打包各種文件的相關(guān)資料,其中包括處理css文件、less文件、scss文件、url地址以及ES6高級(jí)語法的方法,需要的朋友可以參考下2019-09-09JS使用數(shù)組實(shí)現(xiàn)的隊(duì)列功能示例
這篇文章主要介紹了JS使用數(shù)組實(shí)現(xiàn)的隊(duì)列功能,結(jié)合實(shí)例形式分析了javascript基于數(shù)組的隊(duì)列定義、元素添加、讀取等相關(guān)操作技巧,需要的朋友可以參考下2019-03-03JavaScript中獲取當(dāng)前時(shí)間yyyymmddhhmmss的六種實(shí)現(xiàn)方式
js中提供了一個(gè)Date對(duì)象供我們獲取當(dāng)前時(shí)間,下面這篇文章主要給大家介紹了關(guān)于JavaScript中獲取當(dāng)前時(shí)間yyyymmddhhmmss的六種實(shí)現(xiàn)方式,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12微信小程序三級(jí)聯(lián)動(dòng)地址選擇器的實(shí)例代碼
本篇文章主要介紹了微信小程序三級(jí)聯(lián)動(dòng)地址選擇器的實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07微信小程序?qū)崿F(xiàn)監(jiān)聽頁面滾動(dòng)
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)監(jiān)聽頁面滾動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06js如何構(gòu)造elementUI樹狀菜單的數(shù)據(jù)結(jié)構(gòu)詳解
由于業(yè)務(wù)需要,要求實(shí)現(xiàn)樹形菜單,且菜單數(shù)據(jù)由后臺(tái)返回,下面這篇文章主要給大家介紹了關(guān)于js如何構(gòu)造elementUI樹狀菜單的數(shù)據(jù)結(jié)構(gòu)的相關(guān)資料,需要的朋友可以參考下2021-05-05解決在layer.open中使用時(shí)間控件laydate失敗的問題
今天小編就為大家分享一篇解決在layer.open中使用時(shí)間控件laydate失敗的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09