js做过渡效果是怎么样做的?

小白对js不太熟悉,那么请问,我如果js来做点击按钮,然后div经过5s移动到右边500px的地方,这种效果的制作思路是啥?

是先写好一个class,让这些效果完全都达到了,然后用js来切换className么?

但是在点击事件里面,把所有的css效果写进去? QUQ


HUH函数
浏览 649回答 1
1回答

汪汪一只猫

<!DOCTYPE html><html><head><meta charset="utf-8">&nbsp;<title></title><style>&nbsp;div{&nbsp; &nbsp; width:100px;&nbsp; &nbsp; height:100px;&nbsp; &nbsp; background:red;&nbsp; &nbsp; transition:all 5s;}div:hover{&nbsp; &nbsp; width:300px;}&nbsp; &nbsp; #div1{&nbsp; &nbsp; &nbsp; &nbsp; position:relative;&nbsp; &nbsp; &nbsp; &nbsp; width:100px;&nbsp; &nbsp; &nbsp; &nbsp; height:100px;&nbsp; &nbsp; }&nbsp; &nbsp; #div2{&nbsp; &nbsp; &nbsp; &nbsp; position:absolute;&nbsp; &nbsp; &nbsp; &nbsp; width:100%;&nbsp; &nbsp; &nbsp; &nbsp; height:100%;&nbsp; &nbsp; &nbsp; &nbsp; background:#0f0;&nbsp; &nbsp; }&nbsp; &nbsp; #div2.div2{&nbsp; &nbsp; &nbsp; &nbsp; width:200%;&nbsp; &nbsp; &nbsp; &nbsp; height:200%;&nbsp; &nbsp; }</style></head><body><input type='button' id='btn1'/><p><b>注意:</b>该实例无法在 Internet Explorer 9 及更早 IE 版本上工作。</p><div></div>&nbsp; &nbsp; <div id='div1'>&nbsp; &nbsp; &nbsp; &nbsp; <div id='div2'></div>&nbsp; &nbsp; </div><p>鼠标移动到 div 元素上,查看过渡效果。</p></body>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; window.onload=function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var oBtn=document.getElementById("btn1"),oDiv=document.getElementById("div2");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oBtn.onclick=function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oDiv.className='div2';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; </script></html>给你个简单的demo看完就会了,这也只是方法中的一种,方法还有很多!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript