javascript - 如何将速度参数分配给回调函数

标签 javascript jquery css

我一直在尝试弄清楚如何将速度分配给回调函数,但没成功。

在这个程序中。

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');
        });
    });
});

FIDDLE

关于javascript - 如何将速度参数分配给回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22893745/

相关文章:

javascript - 响应自定义事件的更好方法

html - 如何在缩小屏幕尺寸时均匀地修剪图像的左右两侧? (@媒体查询)

html - 向 HTML 中的类动态添加属性

javascript - .包括未找到期间

javascript - Swiper.IO 分页点现在显示

javascript - 限制文本框以特定字符开头

javascript - 检查动态添加的元素是否准备好

Jquery 删除和更改 html 标签

jquery - body 上的 CSS 变换搞乱了固定的导航栏

javascript - 使用 jquery 将值附加到 asp.net 下拉列表