如何使一个元素从网页的顶部跳到底部再跳回到顶部
How to make an element jump from top to the bottom of the web page and back to the top?
我有一个球形状的元素div。我想做的是,当我刷新页面时,我希望球掉到网页底部,然后反弹到页面顶部。
这是我的jQuery函数,球落在网页的底部
$(document).ready(function(){
$("div").animate({ top: '+=585'}, 400);
});
我使用的方法正确吗?我应该使用slideWon和slideUp吗?
尝试使用jQuery UI.effect()
$(function() {
var div = $("div");
// `elem`: element to apply bounce effect,
// `n`: number of bounce effects to apply to `elem`
var bounce = function bounce(elem, n) {
var fx = function fx(el) {
return (el || $(this))
.effect({
effect: "bounce",
easing: "swing",
duration: 400,
distance: window.innerHeight
- (el.height() + el.offset().top * 1.5),
direction: "down",
times: 1
}).promise()
};
return fx(elem).then.apply(elem, $.map(Array(n - 1), function() {
return fx(elem)
}));
};
bounce(div, 1).then(function(el) {
// do stuff when bounce effect complete
console.log("complete", el)
});
});
div {
position: relative;
width: 100px;
height: 100px;
background: rgb(212, 98, 44);
border: 2px solid navy;
border-radius: 50%;
}
<link href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"
rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div></div>
利用jQuery的动画可链接性。此外,您可能不应该假设静态值585
适用于每个屏幕大小。我建议使用计算值来生成偏移,检查这个fiddle:
$(document).ready(function () {
var viewportH = $(window).height();
var elem = $('div');
var elemH = elem.height();
elem.animate({
top: '+=' + (viewportH - elemH) // bottom of screen
}, 400).animate({
top: '-=' + (viewportH - elemH) // original position
});
});
使用此HTML:
<div id="myDiv" class="myRelativeDiv">test</div>
第一步是将div的位置设置为"相对":
.relative {
position:relative;
}
第二步是用Jquery制作动画(你可以链接许多animate
):
$(function() {
$("#myDiv").animate({ top: '+=585'}, 400).animate({ top: '0'}, 400);
});
Js出价
$(document).ready(function(){
$("div").animate({ top: '+=585'}, 400);
setTimeout(
function()
{
$("div").animate({ top: '-=585'}, 400);
}, 400);
});
相关文章:
- 如何将图表放在顶部和底部
- 滚动到顶部或底部后的简单效果
- 如何使一个元素从网页的顶部跳到底部再跳回到顶部
- 垂直与特定边距的顶部和底部对齐
- HTML导航在屏幕底部,并粘贴到顶部
- 粘贴Bootstrap在粘贴底部到达并滚动回顶部后闪烁
- 我们如何在fullPage.js中为不同的部分制作不同的填充顶部和底部值
- 当在堆栈底部添加ImageView时,我想删除RelativeLayout中的顶部ImageView,但它正在删除整个布
- 为什么javascript在页面底部或有时在页面顶部运行
- 使手风琴/可折叠中的 DIV 保持固定在页面的顶部和底部
- 如何使用 jQuery 检测元素的顶部靠近浏览器底部
- 如何在滚动到底部后在页脚上方设置“返回顶部”
- Div 在页眉到达其顶部时开始滚动,当其底部到达页脚时停止滚动
- 当在 Ul 上使用向上/向下箭头到达溢出自动项目的顶部/底部时显示隐藏的项目
- 检测(事件)尝试在使用JavaScript或AngularJS页面顶部/底部时向上/向下滚动
- 滚动到顶部/底部按钮错误
- Skrollr相对于顶部底部在chrome中较早着火
- 调整边缘-顶部/底部与屏幕高度成比例
- 恼人的不必要的溢出和页边空白顶部/底部没有'不起作用
- 当内部元素的滚动位置达到顶部/底部时,防止父元素滚动