day 16 DOM案例

百度换肤

点击更换皮肤,会弹出图片框,点击图片,背景会换成所点击的图片。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>百度换肤</title>
</head>
<style>
    *{
        margin: 0;
        padding: 0;
        list-style: none;
    }
    body{
        padding-top: 60px;
        width: 100vw;
        height: 100vh;
        background-color: greenyellow;
        background-position: center center;
        background-size: cover;
        background-attachment: fixed;
    }
    nav{
        height: 60px;
        width: 100%;
        background-color: rgba(200, 200, 200, .8);
        top: 0;
        position: fixed;
    }
    nav p{
        height: 60px;
        line-height: 60px;
        font-size: 25px;
        text-align: right;
    }
    .content{
        width: 200px;
        height: 1000px;
        background-color: pink;
        margin: 0 auto;
        text-align: center;
    }
    .wrap{
        display: none;
        width: 100vw;
        height: 100vh;
        background-color: rgba(0, 0, 0, .1);
        position: fixed;
        justify-content: center;
        align-items: center;
        top: 0;
    }
    .wrap .alert{
        width: 800px;
        height: 300px;
        background-color: #fff;
        position: absolute;
        display: flex;
        justify-content: space-around;
        align-items: center;

    }
    .wrap .alert strong{
        position: absolute;
        top: 0;
        right: 5px;

    }
    .wrap .alert strong:hover{
        cursor: pointer;
        color: skyblue;
    }
    .alert img{
        width: 150px;
        transition: .3s;
    }
    .alert img:hover{
        transform: scale(1.2);
    }
    .checked{
        outline: 2px solid tomato;
    }
    .wrap .alert span{
        position: absolute;
        height: 30px;
        line-height: 30px;
        font-size: 18px;
        /* background-color: blue; */
        bottom: 10px;
    }
</style>
<body>
    <nav>
        <p>更换皮肤</p>
    </nav>
    <p class="content">这是内容</p>
    <div class="wrap">
        <div class="alert">
            <strong>X</strong>
            <img src="https://t7.baidu.com/it/u=508006830,4042443322&fm=193&f=GIF" alt="">
            <img src="https://t7.baidu.com/it/u=3029654684,900514866&fm=193&f=GIF" alt="">
            <img src="https://t7.baidu.com/it/u=1451800419,4152544319&fm=193&f=GIF" alt="">
            <img  src="https://t7.baidu.com/it/u=4272803060,4157549139&fm=193&f=GIF" alt="">
            <img  src="https://t7.baidu.com/it/u=604672937,615186004&fm=193&f=GIF" alt="">
            <span>不使用皮肤</span>
        </div>
    </div>
</body>
<script>
    var body = document.querySelector("body");
    var pGh = document.querySelector("nav p");
    var dWrap = document.querySelector(".wrap");
    var del = document.querySelector(".alert strong");
    var imgs = document.querySelectorAll("img");
    var pp = document.querySelector(".alert span");
    var index = -1;
    pGh.onclick = function(){
        dWrap.style.display = "flex";
    }
    del.onclick = function(){
        dWrap.style.display = "none"
    }
    for(var i=0;i<imgs.length;i++){
        imgs[i].xiabiao = i;
        imgs[i].onclick = function(){
           
            if(index === this.xiabiao){
                return ;
            }
            body.style.backgroundImage = `url(${this.src})`;
           

           if(index != -1){
            imgs[index].className = "";
           }
           this.className = "checked";
           index = this.xiabiao;



        }
    }
    pp.onclick = function(){
        // console.log(this)
        body.style.backgroundImage = "";
        imgs[index].className = "";
        index = -1;
    }
</script>
</html>

 手风琴效果

第一张图片默认为展开图片,点击其他图片时,已经展开的图片关闭,所点击的图片展开,并更换相应的背景图。

 

​
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>手风琴</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            list-style: none;
        }
        body{
            width: 100vw;
            height: 100vh;
            background-image: url(https://img2.baidu.com/it/u=2543265134,3617543792&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=375);
            background-size: cover;
            background-position: center center;
        }
        .wrap{
            width: 800px;
            height: 400px;
            margin: 0 auto;
            background-color: pink;
        }
        .wrap div{
            width: 80px;
            height: 100%;
            float: left;
            background-size: cover;
            background-position: center center;
            transition: .3s;
        }
        .wrap .open{
            width: 480px;
            /* height: 100%; */
        }
        .wrap>div p{
            width: 80px;
            height: 100%;
            background-color: rgba(255, 255, 255, .5);
            font-size: 50px;
            color: white;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="wrap">
        <div class="open" style="background-image: url(https://img2.baidu.com/it/u=2543265134,3617543792&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=375)">
            <p>长白山</p>
        </div>
        <div style="background-image: url(https://img2.baidu.com/it/u=3872400608,2772209323&fm=253&fmt=auto&app=138&f=JPEG?w=749&h=500)">
            <p>厦门</p>
        </div>
        <div style="background-image: url(https://img1.baidu.com/it/u=2995749024,1292843217&fm=253&fmt=auto&app=138&f=JPEG?w=747&h=500)">
            <p>日落</p>
        </div>
        <div style="background-image: url(https://img0.baidu.com/it/u=1875111408,2138437588&fm=253&fmt=auto&app=138&f=JPEG?w=680&h=383)">
            <p>夜景</p>
        </div>
        <div style="background-image: url(https://img1.baidu.com/it/u=2995749024,1292843217&fm=253&fmt=auto&app=138&f=JPEG?w=747&h=500)">
            <p>雪景</p>
        </div>
    </div>
</body>
<script>
    var body = document.getElementsByTagName("body")[0];
    var divs = document.querySelectorAll(".wrap>div");
    
    var showIndex = 0;

    for(var i=0;i<divs.length;i++){
        // console.log(i);
        divs[i].index = i;
        divs[i].onclick = function(){
            // console.log(this.index)
           
            if(showIndex === this.index){
                return ;
            }
            divs[showIndex].className = "";
            this.className = "open";
            showIndex = this.index;

            body.style.backgroundImage = this.style.backgroundImage;
        }
    }
</script>
</html>

​

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值