打卡GitHub50天50个前端小项目

day-1(手风琴)

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>Document</title>
</head>
<body>
    <ul>
        <li class="open" style="background-image: url(https://images.unsplash.com/photo-1558979158-65a1eaa08691?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80);"><p>背景1</p> </li>
        <li style="background-image: url(https://images.unsplash.com/photo-1572276596237-5db2c3e16c5d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80);"><p>背景2</p> </li>
        <li style="background-image: url(https://images.unsplash.com/photo-1507525428034-b723cf961d3e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1353&q=80);"><p>背景3</p> </li>
        <li style="background-image: url(https://images.unsplash.com/photo-1551009175-8a68da93d5f9?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1351&q=80);"><p>背景4</p> </li>
    </ul>
   
</body>
</html>

CSS块

*{
            margin: 0;
            padding: 0;
            list-style: none;
        }
        body{
            height: 100vh;
            background-image: url(https://images.unsplash.com/photo-1558979158-65a1eaa08691?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80) ;
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
            margin: 0;
            background-size: cover;

        }
        ul{
            display: block;
            width: 800px;
            height: 400px;
            border-radius: 30px;
            overflow: hidden;
        }
        li{
            width: 80px;
            height: 100%;
            float: left;
            background-size:cover;
            transition: .5s;
            cursor: pointer;
            border-radius: 20px;
            overflow: hidden;

            
        }
        li p{
            /* position: absolute; */
            width: 80px;
            height: 100px;
            background-color: rgba(255,255,255,.5);
            color: white;
            font-size: 35px;
            border-radius: 20px;
            /* margin-left: 20px; */
        }
        .open{
            width: 480px;
        }

JS块


        
// --------------------法1----------------------------
        const lis = document.getElementsByTagName('li');
        let showIndex = 0;                              //当前open展开的下标;
        for(let i =0;i<lis.length;i++){
            lis[i].index = i;
            lis[i].onclick = function(){
                for(let j = 0;j<lis.length;j++){
                    if(showIndex === this.index){
                    return ;}
                    lis[j].className = ''
                }
                this.className = 'open'
                showIndex = this.index                  //修改showindex   
                document.body.style.backgroundImage = this.style.backgroundImage;
            }
        }
//-------------------法2---------------------------------
// let showIndex = 0;                              //当前open展开的下标;
        // for(let  i = 0;i<lis.length;++i){
        //     lis[i].index = i;
        //     lis[i].onclick = function(){
        //         if(showIndex === this.index){
        //             return ;
        //         }
        //         //法2  推荐使用 
        //         lis[showIndex].className = '';          // 动态添加下表
        //         this.className = 'open';                //展开点击 的li
        //         showIndex = this.index                  //修改showindex   
        //         document.body.style.backgroundImage = this.style.backgroundImage;
        //     }
        // }
// -----------------法3----------------------------
// const lis = document.querySelectorAll('li');
// lis.forEach(li=>{
//     li.addEventListener('click',()=>{
//         removeActiveClasses()
//         li.classList.add('open')
//         document.body.style.backgroundImage = li.style.backgroundImage
//     })
// })
// function removeActiveClasses(){
//     lis.forEach(li=>{
//         li.classList.remove('open')

//     })
// }

效果图

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码到无能为力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值