我一直在尝试弄清楚如何将速度分配给回调函数,但没成功。
在这个程序中。
HTML:
<button type="button" id="trigger">Try</button>
<div id="box"></div>
<div id="bottom"></div>
CSS:
body
{
position: absolute;
background-color: white;
height: 1000%;
}
#box
{
position: absolute;
height: 300px;
width: 400px;
top: 100px;
left: 0px;
margin: 0px;
padding: 0px;
border-color: black;
background-color: black;
opacity: 0;
}
#bottom
{
position: absolute;
bottom: 0px;
height: 100px;
width: 100%;
background-color: black;
}
JS:
$(document).ready(function () {
$("#trigger").click(function () {
var ele = document.getElementById('bottom');
var pos = ele.getBoundingClientRect();
var x = pos.left;
var y = pos.top;
$("#box").animate({
left: "100px",
opacity: "1"
}, "slow", function () {
window.scrollTo(x, y);
}, "slow");
});
});
从回调中删除速度后,它可以工作但没有成功。 如何为其分配速度,提前致谢。
fiddle :http://jsfiddle.net/9C8kr/
最佳答案
你加了两次速度,最后一次无效。
如果你想为滚动设置动画,那就这样做
$(document).ready(function () {
$("#trigger").click(function () {
var ele = document.getElementById('bottom');
var pos = ele.getBoundingClientRect();
var x = pos.left;
var y = pos.top;
$("#box").animate({
left: "100px",
opacity: "1"
}, "slow", function () {
$('html, body').animate({
scrollLeft : x,
scrollTop : y
}, 'slow');
});
});
});
关于javascript - 如何将速度参数分配给回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22893745/