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>以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于js實(shí)現(xiàn)數(shù)組相鄰元素上移下移
這篇文章主要介紹了基于js實(shí)現(xiàn)數(shù)組相鄰元素上移下移,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
基于javascript實(shí)現(xiàn)按圓形排列DIV元素(三)
本篇文章主要介紹基于javascript實(shí)現(xiàn)按圓形排列DIV元素的方法,此文著重于介紹怎樣實(shí)現(xiàn)圖片按橢圓形轉(zhuǎn)動(dòng),需要的朋友來看下吧2016-12-12
JS使用正則表達(dá)式實(shí)現(xiàn)常用的表單驗(yàn)證功能分析
這篇文章主要介紹了JS使用正則表達(dá)式實(shí)現(xiàn)常用的表單驗(yàn)證功能,結(jié)合實(shí)例形式分析了JS基于正則表達(dá)式的表單驗(yàn)證功能原理、實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下2020-04-04
讓Firefox支持event對(duì)象實(shí)現(xiàn)代碼
FireFox并沒有 window.event ,所以在FireFox下編寫事件處理函數(shù)是很麻煩的事。如果要得到 event 對(duì)象,就必須要聲明時(shí)間處理函數(shù)的第一個(gè)參數(shù)為event2009-11-11
原生JS實(shí)現(xiàn)獲取及修改CSS樣式的方法
這篇文章主要介紹了原生JS實(shí)現(xiàn)獲取及修改CSS樣式的方法,結(jié)合實(shí)例形式簡(jiǎn)單分析了JavaScript針對(duì)頁面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-09-09

