鼠标移入搭建盒子效果

通过这个练习可以进一步锻炼思维,并普及一下nth-of-type和nth-child的区别。

一、效果

 二、具体代码(注意每个li的位置)

<style>
    *{
        padding: 0;
        margin: 0;
        list-style: none;
    }
    html,body{
        width:100%;
        height: 100%;
        background-color: black;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    div{
        width:1000px;
        height: 500px;
        border: 1px solid red;
        display: flex;
        justify-content: center;
        align-items: center;
        perspective: 1000px;
    }
    ul{
        width: 300px;
        height: 300px;
        position: relative;
        transform-style:preserve-3d ;
        transition: all 2s;
    }
    li{
        width: 100%;
        height: 100%;
        position: absolute;
        top:0;
        left:0;
        transition: all 2s;
    }
    div:hover li:first-of-type{
        background-color: blueviolet;
        transform: translateZ(-150px);
        opacity: 0.8;
    }
    div:hover li:nth-of-type(2){
        background-color: brown;
        transform: translateX(-150px) rotateY(90deg);
        opacity: 0.8;
    }
    div:hover li:nth-of-type(3){
        background-color: rgb(21, 218, 21);
        transform: translateX(150px) rotateY(90deg);
        opacity: 0.8;
    }
    div:hover li:nth-of-type(4){
        background-color: rgb(11, 171, 199);
        transform: translateY(-150px) rotateX(90deg);
        opacity: 0.8;
    }
    div:hover li:nth-of-type(5){
        background-color: rgb(219, 29, 178);
        transform: translateY(-150px) rotateX(90deg);
        opacity: 0.8;
    }
    div:hover li:last-of-type{
        background-color: rgb(221, 43, 11);
        transform: translateZ(150px);
        opacity: 0.8;
    }
    div:hover ul{
        transform: rotate3d(1,1,1,360deg);
    }
    

</style>
<body>
    <div>
        <ul>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
</body>

三、nth-of-type和nth-child区别

nth-child 与nth-of-type 均可以选择父元素下对应的子元素。

ele:nth-of-type(n)是指父元素下第n个ele元素,而ele:nth-child(n)是指父元素下第n个元素且这个元素为ele

简单的说,nth-of-type就是取当前元素的兄弟元素的第n个,而nth-child取的是当前元素的第n个节点的当前元素。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

爱吃彩虹吐司的安琪拉

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

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

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

打赏作者

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

抵扣说明:

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

余额充值