这个关键在于css的伪类选择器:target
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>css触发点击动画</title> <style> .btn1,.box{ display:flex; position: relative; } .btn1>a{ display: block; margin:5px; width: 100px; height:100px; background: #a58c4d; } #a,#b,#c{ margin:5px; width:150px; height:100px; background:#662; position: absolute; top:0; } #a{ left:0px; } #b{ left:160px; } #c{ left:320px; } #a:target,#b:target,#c:target{ width:150px; height:100px; background: #e9e8e7; -webkit-animation-name:slide; -webkit-animation-duration: 2s; -webkit-animation-iteration-count: 1; /*animation-fill-mode:forwards;*/ } @-webkit-keyframes slide{ 0% {top:0px;} 50% {top: 300px;} 100% {top: 0px;} } </style> </head> <body> <div> <a href="#a">按钮a</a> <a href="#b">按钮b</a> <a href="#c">按钮c</a> </div> <div> <div id="a">a</div> <div id="b">b</div> <div id="c">c</div> </div> </body> </html>
前端开发QQ群:577185192