亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

絕對定位的動畫子 div 超出了其父級的邊界,如何修復?

絕對定位的動畫子 div 超出了其父級的邊界,如何修復?

侃侃無極 2024-01-11 16:38:26
我正在對父 div 中絕對定位的子 div 的左側位置進行動畫處理。動畫雖然確實有效,但當子 div 達到 50% ( 時50% {left:100%;},它會超出其父級的邊界。A.) 為什么它只發生在 left: 100% 而不是 0% 時?B.) 如何讓子項在父項內移動 - 不離開父項右側?這是我的代碼:#parent {  border:1px solid red;  width:500px;  height:200px;  margin:100px auto;  position:relative;}/* The element to apply the animation to */#child {  width:100px;  height:100px;  border:1px solid blue;  position:absolute;  -webkit-animation:animatedPos 20s linear infinite;  -o-animation:animatedPos 20s linear infinite;  -moz-animation:animatedPos 20s linear infinite;  animation:animatedPos 20s linear infinite;}/* The animation code */@-webkit-keyframes animatedPos {  0% {left:0%;}  50% {left:100%;}  100% {left:0%;}}@-o-keyframes animatedPos {  0% {left:0%;}  50% {left:100%;}  100% {left:0%;}}@-moz-keyframes animatedPos {  0% {left:0%;}  50% {left:100%;}  100% {left:0%;}}@keyframes animatedPos {  0% {left:0%;}  50% {left:100%;}  100% {left:0%;}}<div id="parent">  <div id="child"></div></div>
查看完整描述

4 回答

?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

改變


@keyframes animatedPos {

  0% {left:0%;}

  50% {left:100%;}

  100% {left:0%;}

}


*{

  box-sizing:border-box;

}

@keyframes animatedPos{

  0%{

    left:0;

  }

  50%{

    left:calc(100% - 100px);

  }

  100%{

    left:0;

  }

}


查看完整回答
反對 回復 2024-01-11
?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

#parent {

  border:1px solid red;

  width:500px;

  height:200px;

  margin:100px auto;

  position:relative;

}


#uncle {

  border:0px solid silver;

  width:400px;

  height:200px;

  margin:0px auto;

  position:absolute;

}


/* The element to apply the animation to */

#child {

  width:100px;

  height:100px;

  border:1px solid blue;

  position:absolute;

  -webkit-animation:animatedPos 20s linear infinite;

  -o-animation:animatedPos 20s linear infinite;

  -moz-animation:animatedPos 20s linear infinite;

  animation:animatedPos 20s linear infinite;

}


/* The animation code */

@-webkit-keyframes animatedPos {

  0% {left:0%;}

  50% {right:100%;}

  100% {left:0%;}

}


@-o-keyframes animatedPos {

  0% {left:0%;}

  50% {left:100%;}

  100% {left:0%;}

}


@-moz-keyframes animatedPos {

  0% {left:0%;}

  50% {left:100%;}

  100% {left:0%;}

}


@keyframes animatedPos {

  0% {left:0%;}

  50% {left:100%;}

  100% {left:0%;}

}

<div id="parent">

<div id="uncle">

  <div id="child"></div>

</div>

</div>

這是一種解決方法,添加其他 Div(uncle) 減去動畫寬度。該問題是由于使用軸移動的動畫僅考慮一個點,而不是動畫寬度。

應該是跨瀏覽器的。


查看完整回答
反對 回復 2024-01-11
?
喵喔喔

TA貢獻1735條經驗 獲得超5個贊

您應該考慮子項的寬度并將其從 50% 減少。



查看完整回答
反對 回復 2024-01-11
?
陪伴而非守候

TA貢獻1757條經驗 獲得超8個贊

您可以如下設置 50%。80% 使用父級和子級指定的寬度計算 ((500px-100px)/500px)


@keyframes animatedPos {

 0% {

  left: 0%;

 }

 50% {

  left: 80%;

 }

 100% {

  left: 0%;

 }

}


查看完整回答
反對 回復 2024-01-11
  • 4 回答
  • 0 關注
  • 222 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號