gpt4 book ai didi

jQuery 动画边距问题

转载 作者:行者123 更新时间:2023-12-01 04:53:39 27 4
gpt4 key购买 nike

我正在应客户的请求使用 jQuery 创建一个介绍动画。此代码适用于大多数浏览器,但 IE8 和 Safari (PC) 除外,其中 div 跳转到其最终位置而不是动画。我已经研究这个问题几天了:http://jsfiddle.net/evolve/5g2FQ/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>

<style type="text/css">
body, html {margin: 0;}
#navigation-next-top {background: red; position: absolute; width: 20px; height: 20px; top: 40px; right: 0; bottom: 0; left: 0; margin: auto;}
#navigation-next-right {background: orange; position: absolute; width: 20px; height: 20px; top: 0; right: 40px; bottom: 0; left: 0; margin: auto;}
#navigation-next-bottom {background: yellow; position: absolute; width: 20px; height: 20px; top: 0; right: 0; bottom: 40px; left: 0; margin: auto;}
#navigation-next-left {background: green; position: absolute; width: 20px; height: 20px; top: 0; right: 0; bottom: 0; left: 40px; margin: auto;}
</style>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".logo").click(function(){
$("#navigation-next-top").animate({"top": "40px", "marginTop": "0"}, 2000);
$("#navigation-next-right").animate({"right": "40px", "marginRight": "0"}, 2000);
$("#navigation-next-bottom").animate({"bottom": "40px", "marginBottom": "0"}, 2000);
$("#navigation-next-left").animate({"left": "40px", "marginLeft": "0"}, 2000);
});
});
</script>

</head>
<body>

<div id="navigation-next-top" class="logo"></div>
<div id="navigation-next-right" class="logo"></div>
<div id="navigation-next-bottom" class="logo"></div>
<div id="navigation-next-left" class="logo"></div>

</body>
</html>

我尝试了很多方法,如果我更改 margin: auto; 就会出现这种情况到像 200px 这样动画就可以正常工作,但是 margin: auto;是什么首先将我的 div 定位在中心。

我尝试过使用 % ,但这需要具有响应性,并且 % 在某些浏览器中会出现对齐问题。

使用 jQuery 2 在 IE8 中会出现 JS 错误,因此我选择了 1.9.1。

我只是希望它在所有浏览器中从头到尾都有动画效果。

最佳答案

请使用以下标记 -

CSS

* { margin:0; padding:0 } /* use css reset instead of using * */
html, body { height:100%; width:100% }
.box { width:20px; height:20px; position:absolute; left:50%; top:50%; margin-left:-20px; margin-top:-20px }

.top { background-color:#09C; margin-top:-40px }
.right { background-color:#C66; margin-left:0px }
.bottom { background-color:#936; margin-top:0px }
.left { background-color:#000; margin-left:-40px }

HTML

<div class="box top"></div>
<div class="box right"></div>
<div class="box bottom"></div>
<div class="box left"></div>

JS

$(document).ready(function(){
var timing = 2000;

$('.box').click(function(){
$('.top').animate({ top: '100%' }, timing);
$('.bottom').animate({ top: '20px' }, timing);
$('.left').animate({ left: '100%' }, timing);
$('.right').animate({ left: '20px' }, timing);
});
});

请参阅此 jsFiddle - http://jsfiddle.net/rsAGv/

关于jQuery 动画边距问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16707897/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com