css3 transition的触发方式

第一种: 通过伪类元素触发

	<style>
		.box{
      		width: 100px;
      		height: 100px;
      		background-color: blueviolet;
      		transition: width 1s linear .5s;
    	}
    	.box:hover{
      		width: 400px;
    	}
	</style>
	<div class="box">
    </div>

第二种: 通过JS触发

通过js添加必须有一定的延迟(延迟去掉无效果)来改变元素的样式

<style>
	.box{
      width: 100px;
      height: 100px;
      background-color: blueviolet;
      transition: width 1s linear .5s;
    }
    .box1{
      width: 400px;
    }
</style>
<div class="box">    
</div>

<scrpit>
	setTimeout(() => {
      let element = document.getElementsByClassName('box')[0];
      element.classList.add('box1')
    }, 1) 
</scrpit>
发布了5 篇原创文章 · 获赞 5 · 访问量 121

猜你喜欢

转载自blog.csdn.net/qq_37061326/article/details/105300283