JS搞怪小案例

看小红书觉得有趣就做了一份

主要用到的就是给元素添加事件,并且复习CSS中的定位基础

<!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>
        .box {
            width: 500px;
            height: 300px;
            background-color: rgb(185, 181, 181);
            margin: 100px auto;
            position: relative;
        }
        
        .box2 {
            width: 500px;
            height: 300px;
            margin: 0px auto;
            /* 会继承父盒子的margin */
            position: absolute;
            top: 0px;
            left: 0px;
            background-color: skyblue;
        }
        
        .tu {
            width: 400px;
            height: 130px;
            margin-left: 20px;
            position: absolute;
            top: 60px;
            left: 20px;
            font-size: 20px;
            font-weight: 800;
        }
        
        .box2 img {
            width: 150px;
            height: 150px;
            vertical-align: text-bottom
            /* 注意用到图片与文字的对齐,不能让图片浮动起来 */
        }
        
        .qe1 {
            font-size: 30px;
            text-align: center;
            padding-top: 50px
        }
        
        .close {
            width: 30px;
            height: 30px;
            /* background-image: url(关闭.png);
            background-position: center;
            background-repeat: no-repeat; */
            background-color: white;
            font-size: 20px;
            position: absolute;
            top: 0px;
            right: 0px;
            border: 1px solid grey
        }
        
        .ans1,
        .ans2 {
            font-size: 20px;
            width: 100px;
            height: 30px;
            text-align: center;
            line-height: 30px;
        }
        
        .ans1 {
            position: absolute;
            left: 50px;
            bottom: 80px;
        }
        
        .ans2 {
            position: absolute;
            right: 50px;
            bottom: 80px;
        }
        
        .xbox {
            width: 400px;
            height: 200px;
            background-color: red;
            position: absolute;
            top: 50px;
            left: 50px;
            text-align: center;
        }
        
        .ch {
            font-size: 20px;
        }
        
        .ch2 {
            width: 150px;
            height: 30px;
            margin-top: 40px;
        }
    </style>
</head>

<body>
    <div class="box">
        <p class="qe1">你觉得你<strong>漂亮</strong>吗</p>
        <button class="close">x</button>
        <button class="ans1">我最漂亮</button>
        <button class="ans2">不觉得</button>
        <div class="xbox" style="display:none">
            <p class="ch">点我没用,快点选</p>
            <button class="ch2">回去重选</button></div>
        <div class="box2" style="display:none">
            <div class="tu"> <img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fp5.itc.cn%2Fimages01%2F20201121%2F80d1111d323e4235a919a53658f1283b.jpeg&refer=http%3A%2F%2Fp5.itc.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648173756&t=d48c7d99df838b33a5450b32c253a0f0"
                    alt=""> 嗯!!!!你自己知道就好
            </div>

        </div>
    </div>


    <script>
        var ans1 = document.querySelector('.ans1')
        var ans2 = document.querySelector('.ans2')
        console.log(ans2.innerText)
        ans1.onmouseover = function() {
            if (ans1.innerHTML == '我最漂亮') {
                ans1.innerHTML = '不觉得'
                ans2.innerHTML = '我最漂亮'
            }
        }
        ans1.onmouseout = function() {
            if (ans1.innerHTML == '不觉得') {
                ans1.innerHTML = '我最漂亮'
                ans2.innerHTML = '不觉得'
            }
        }
        var box2 = document.querySelector('.box2')
        ans1.onclick = function() {
            box2.style.display = 'block'
        }
        ans2.onclick = function() {
            box2.style.display = 'block'
        }
        var close = document.querySelector('.close')
        var xbox = document.querySelector('.xbox')
        close.onclick = function() {
            xbox.style.display = 'block'
        }
        var ch2 = document.querySelector('.ch2')
        ch2.onclick = function() {
            xbox.style.display = 'none'
        }
    </script>
</body>

</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一夕ξ

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

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

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

打赏作者

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

抵扣说明:

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

余额充值