[英]Animating an image vertically - jQuery
我一直在尝试通过使用jQuery对其进行垂直动画处理来使图像看起来好像是漂浮的。 做完一些挖掘之后,我遇到了这个问题: 反复使div上下动画,这正是我所需要的。 将代码应用到我的网站后,我仍然无法弄清楚图像为什么不像这里的示例那样。 这是我到目前为止的代码。
HTML
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
<link href="https://fonts.googleapis.com/css?family=Noto+Sans" rel="stylesheet">
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type ="text/javascript" src="script.js"></script>
<!-- Checking to see if running properly
<script>
window.onload = function() {
if (window.jQuery) {
// jQuery is loaded
alert("Yeah!");
} else {
// jQuery is not loaded
alert("Doesn't Work");
}
}
</script> -->
</head>
<body>
<table id="wrapper">
<tr>
<td><img src="PixelBuddah.png" class="bouncer" alt="PixelBuddah" /></td>
</tr>
</table>
<footer>
<p>...</p>
</footer>
</body>
</html>
CSS
html, body, #wrapper {
height:100%;
width: 100%;
margin: 0;
padding: 0;
border: 0;
}
#wrapper td {
vertical-align: middle;
text-align: center;
}
body {
background-color: #F5E9D3;
}
footer {
display: inline-block;
float: right;
text-align: right;
margin: auto;
margin-right: 1.2em;
font-family: 'Noto Sans', sans-serif;
font-weight: 600;
}
使用Javascript
function loop() {
$('.bouncer').animate({'top': '500'}, {
duration: 1000,
complete: function() {
$('.bouncer').animate({top: 0}, {
duration: 1000,
complete: loop});
}});
$('<div/>').text('exiting loop').appendTo($('.results'));
}
loop();
我对此仍然很陌生,因此我们将不胜感激! 谢谢!
由于要更改图像的位置,因此需要将position属性添加到图像的CSS中。 注意CSS for img标签
function loop() { $('.bouncer').animate({'top': '500'}, { duration: 1000, complete: function() { $('.bouncer').animate({top: 0}, { duration: 1000, complete: loop}); }}); $('<div/>').text('exiting loop').appendTo($('.results')); } loop();
html, body, #wrapper { height:100%; width: 100%; margin: 0; padding: 0; border: 0; } #wrapper td { vertical-align: middle; text-align: center; } body { background-color: #F5E9D3; } footer { display: inline-block; float: right; text-align: right; margin: auto; margin-right: 1.2em; font-family: 'Noto Sans', sans-serif; font-weight: 600; } img { position: absolute; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <body> <table id="wrapper"> <tr> <td><img src="PixelBuddah.png" class="bouncer" alt="PixelBuddah" /></td> </tr> </table> <footer> <p>...</p> </footer> </body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.