我做了一個正方形,使用set和clearInterval移動到其容器的寬度結束。 如果我把寬度添加為數字,它就會停止,但當我添加container.width時,它就不作業了
。var container = document.getElementById('container')
var square = document.getElementById('mySquare')
function theAll(sq,con){
var pos = 0;
var moving = setInterval(move,10)
function move(){
if(pos == con.width){
clearInterval(moving)}。
else{
pos
sq.style.left=pos "px"; } }
}}
theAll( square,container)
#container{
字體-大小。100%。
font-family: Comic Sans MS;
background-color: blanchedalmond;
border-radius: 5px;
padding: 0;
width: 100%。
高度: 60%。
display: flex;
flex-direction: column;
}
#mySquare{
width: 5%。
高度。5%。
background-color: blueviolet;
position: relative;
uj5u.com熱心網友回復:
你可以嘗試用con.offsetWidth替換con.width
示例 https://codepen.io/julien180/pen/JjJvqEd?editors=1111
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/326631.html
標籤:
