JS07-DOM-class案例

案例

倒计时

<!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>
    <textarea name="" id="" cols="30" rows="10">
        用户注册协议
        Lorem ipsum dolor sit, amet consectetur adipisicing elit. Quibusdam, voluptate. Dignissimos harum error magni beatae, itaque delectus! Tempora impedit dolor deleniti! Totam aliquid repellendus asperiores hic obcaecati? Doloremque, debitis autem?
    </textarea>
    <br>
    <button class="btn" disabled>已阅读协议(6)   
    </button>
    <script>
        let btn = document.querySelector('.btn')
        let i = 6
        let timer = setInterval(function(){
            i--
            btn.innerHTML = `已阅读协议(${i})`
            if (i === 0){
                clearInterval(timer)
                btn.disabled = false
                btn.innerHTML = `已阅读协议`
            }
        },1000)
    </script>
</body>
</html>

焦点图

Document

1111

随机抽人

<!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>
        div{
            width: 400px;
            height: 200px;
            border: 2px solid black;
            margin: 0 auto;
        }
        h2{
            text-align: center;
        }
        span{
            text-align: center;
            display: block;
            line-height: 50px;
        }
        .start{
            margin-left: 50px;
        }
        .end{
            float: right;
            margin-right: 50px;
        }
    </style>
</head>
<body>
    <div>
        <h2>随机抽人</h2>
        <span>1</span>
        <button class="start">开始</button>
        <button class="end">结束</button>
    </div>
    <script>
        let name = document.querySelector('span')
        let start = document.querySelector('.start')
        let end = document.querySelector('.end')
        function getRandomIntInclusive(min, max) {
            min = Math.ceil(min);
            max = Math.floor(max);
            return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值
        }
        let data = ['刻晴','香菱', '凝光','甘雨','钟离']
        let timer = 0
        let random = 0
        start.addEventListener('click',function(){
            timer = setInterval (function(){
                random = getRandomIntInclusive(0,data.length - 1)
                name.innerHTML = data[random]
            },100)
            start.disabled = true
            end.disabled = false
            if(data.length === 1){
                clearInterval(timer)
                start.disabled = true
                end.disabled = true
            }
        })    
        end.addEventListener('click',function(){
            clearInterval(timer)
            data.splice(random,1)
            end.disabled = true
            start.disabled = false
            if( data.length === 1){
                start.disabled = true
                end.disabled = true
            }
        })    
    </script>
</body>
</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>
    <style>
        *{
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        ul {
            list-style: none;
        }

        .mi{
            position: relative;
            width: 223px;
            margin: 100px auto;
        }

        .mi input {
            width: 223px;
            height: 48px;
            padding: 0 10px;
            font-size: 14px;
            line-height: 48px;
            border: 1px solid rgb(192, 192, 192);
            outline: none;
            transition: all .2s;
        }

        .mi .search {
            border: 2px solid #ff6700;
        }

        .list{
            position: absolute;
            left: 0;
            top: 48px;
            width: 223px;
            border: 1px solid #ff6700;
            border-top: 0;
            background: white;
            display: none;
        }

        .list a {
            display: block;
            padding:  6px 15px;
            font-size: 12px;
            color: #424242;
            text-decoration: none;
        }
    </style>
</head>
<body>
    <div class="mi">
        <input type="search" placeholder="小米">
        <ul class="list">
            <li><a href="#">全部商品</a></li>
            <li><a href="#">小米11</a></li>
            <li><a href="#">小米10s</a></li>
            <li><a href="#">小米笔记本</a></li>
            <li><a href="#">小米笔记本</a></li>
            <li><a href="#">黑鲨4</a></li>
            <li><a href="#">空调</a></li>
        </ul>
    </div>
    <script>
        let search = document.querySelector('input[type="search"]')
        let list = document.querySelector('.list')
        // 获取焦点 focus mouseenter
        search.addEventListener('mouseenter',function(){
            list.style.display = 'block'
            search.classList.add('search')
            
        })
        // 失去焦点 blur mouseleave
        search.addEventListener('mouseleave',function(){
            list.style.display = 'none'
            search.classList.remove('search')
        })
    </script>
</body>
</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>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .w{
            width: 400px;
            height: 200px;
            margin: 0 auto;
        }
        textarea{
            width: 400px;
        }
    </style>
</head>
<body>
    <div class="w">
        <div class="controls">
            <textarea id="area" placeholder="说点什么把..." name="" id="" cols="30" rows="10" maxlength="200"></textarea>
            <div>
                <span class="useCount">0</span>
                <span>/</span>
                <span>200</span>
                <button id="send">发布</button>
            </div>
        </div>
        <div class="contentList">
            <ul></ul>
        </div>
    </div>
    
    <script>
        // 1.获取元素
        let area = document.querySelector('#area')
        let useCount = document.querySelector('.useCount')
        // 2.添加事件
        area.addEventListener('input',function(){
            useCount.innerHTML = area.value.length
        })
    </script>
</body>
</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>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        table{
            border-collapse: collapse;
            border-spacing: 0;
            border: 1px solid #c0c0c0;
            width: 500px;
            margin: 100px auto;
            text-align: center
        }
        th{
            border: 1px solid #d0d0d0;
            color: #404060;
            padding: 10px;
        }
        .allCheck{
            width: 80px;
        } 
        tr:nth-child(1){
            background-color: skyblue;
        }
    </style>
</head>
<body>
    <table>
        <tr>
            <th class="allCheck">
                <input type="checkbox"  id="checkAll"><span class="all">全选</span>
            </th>
            <th>商品</th>
            <th>商家</th>
            <th>价格</th>
        </tr>
        <tr>
            <th>
                <input type="checkbox" name="checl" class="ck">
            </th>
            <th>小米手机</th>
            <th>小米</th>
            <th>1999</th>
        </tr>
        <tr>
            <th>
                <input type="checkbox" name="checl" class="ck">
            </th>
            <th>小米净水器</th>
            <th>小米</th>
            <th>4999</th>
        </tr>
        <tr>
            <th>
                <input type="checkbox" name="checl" class="ck">
            </th>
            <th>小米电视</th>
            <th>小米</th>
            <th>5999</th>
        </tr>
    </table>
    <script>
        let all = document.querySelector('#checkAll')
        let cks = document.querySelectorAll('.ck')
        let span = document.querySelector('span')
        all.addEventListener('click',function(){
            for( let i = 0; i < cks.length; i++){
                cks[i].checked = all.checked
            }
            if (all.checked === true){
                span.innerHTML = '取消'
            }else{
                span.innerHTML = '全选'
            }
        })
        for (let i = 0;i < cks.length; i++ ){
            cks[i].addEventListener('click',function(){
                // 点击一次遍历所有check
                for( let j = 0; j < cks.length; j++){
                    if(cks[j].checked === false) {
                        all.checked = false
                        // 退出循环 结束点击事件
                        return
                    }
                }
                all.checked = true
            })
        }
    </script>
</body>
</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>
    <style>
        div{
            width: 80px;
        }
        input[type=text]{
            width: 50px;
            height: 44px;
            outline: none;
            border: 1px solid #ccc;
            text-align: center;
            border-right: 0;
        }
        input[type=button]{
            width: 22px;
            height: 24px;
            cursor: pointer;
        }
        input{
            float: left;
            border: 1px solid #ccc;
        }
    </style>
</head>
<body>
    <div>
        <input type="text" name="" id="total" value="1" readonly>
        <input type="button" value="+" id="add">
        <input type="button" value="-" id="reduce" disabled>
    </div>
    <script>
        let total = document.querySelector('#total')
        let add = document.querySelector('#add')
        let reduce = document.querySelector('#reduce')
        add.addEventListener('click',function(){
            total.value++
            reduce.disabled = false
        })
        reduce.addEventListener('click',function(){
            total.value--
            if (total.value <= 1){
                reduce.disabled = true
            }
        })
    </script>
</body>
</html>

重点

回调函数

        function fn(){}
        setInterval(fn,1000)
        // fn是回调函数

环境变量this

<body>
    <button>点击</button>
    <script>
        function fn(){
            console.log(this);
        }
        window.fn()
        let btn = document.querySelector('button')
        btn.addEventListener('click',function(){
            console.log(typeof this);
        })
    </script>
</body>

排他思想

<!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>
    <style>
        .color{
            background: skyblue;
        }
    </style>
</head>
<body>
    <button class="color">第1个</button>
    <button>第2个</button>
    <button>第3个</button>
    <button>第4个</button>
    <button>第5个</button>
    <script>
        let btns = document.querySelectorAll('button')
        for (let i = 0; i < btns.length; i++){
            btns[i].addEventListener('click',function(){
                // 干掉所有人
                // for (let j = 0; j < btns.length; j++){
                //     btns[j].classList.remove('color')
                // }
                document.querySelector('.color').classList.remove('color')
                // 我自己
                this.classList.add('color')
            })
        }
    </script>
</body>
</html>

案例tab栏切换

<!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>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .box{
            margin: 0 auto;
            width: 1600px;
            height: 920px;
        }
        .top{
            width: 1600px;
            height: 20px;
        }
        .con{
            width: 1600px;
            height: 900px;
        }
        .con img{
            width: 100%;
            display: none;
            
        }
        ul{
            list-style: none;
            display: flex;
        }
        .box ul .item{
            flex: 1;
            border: 1px solid blue;
            text-align: center;
        }
        .box ul .active{
            background: blue;
            color: aqua;
        }
        .con .active{
            display: block;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="top">
            <ul>
                <li class="item active">1</li>
                <li class="item">2</li>
                <li class="item">3</li>
                <li class="item">4</li>
            </ul>
        </div>
        <div class="con">
            <img src="../../resources/image/background/1.jpg" alt="" class="active">
            <img src="../../resources/image/background/2.jpg" alt="">
            <img src="../../resources/image/background/3.jpg" alt="">
            <img src="../../resources/image/background/4.jpg" alt="">
        </div>
    </div>
    <script>
        let lis = document.querySelectorAll('.item')
        let imgs = document.querySelectorAll('img')
        for(let i = 0 ; i < lis.length; i++){
            lis[i].addEventListener('click',function(){
                document.querySelector('.box ul .active').classList.remove('active')
                this.classList.add('active')
                document.querySelector('.con .active').classList.remove('active')
                imgs[i].classList.add('active')
            })
            
        }
    </script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

colorsZeroz

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

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

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

打赏作者

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

抵扣说明:

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

余额充值