滚动到不在视图中的特定 div
我正在尝试滚动到特定 div 容器的顶部。目前我的代码是将页面内容滚动到视图中。有人知道出了什么问题吗? :) 这是代码:
var openItem;
var showBox = function(item) {
item.removeClass("loading");
var infoBox = $("div.test", item);
// Resize box
item.width(853);
item.height(infoBox.outerHeight());
// Reload Masonry
$('#container').isotope('reLayout');
// Insert close button
$('<a href="#" class="close">Close</a>"')
.prependTo(".test .content")
.click(function (e) {
e.preventDefault();
close(item);
});
// Insert close button
$('<a href="#" class="close">Close</a>"')
.appendTo(".test .content")
.click(function (e) {
e.preventDefault();
close(item);
});
// Fade in test box
setTimeout(function () {
testBox.fadeIn();
// Scroll box into view
$(document).scrollTo(item, 1000);
}, 280);
}
I am trying to scroll to the top of a specific div container. Currently my code is instead scrolling the page content into view. Anybody has an idea how what's wrong? : ) Here's the code:
var openItem;
var showBox = function(item) {
item.removeClass("loading");
var infoBox = $("div.test", item);
// Resize box
item.width(853);
item.height(infoBox.outerHeight());
// Reload Masonry
$('#container').isotope('reLayout');
// Insert close button
$('<a href="#" class="close">Close</a>"')
.prependTo(".test .content")
.click(function (e) {
e.preventDefault();
close(item);
});
// Insert close button
$('<a href="#" class="close">Close</a>"')
.appendTo(".test .content")
.click(function (e) {
e.preventDefault();
close(item);
});
// Fade in test box
setTimeout(function () {
testBox.fadeIn();
// Scroll box into view
$(document).scrollTo(item, 1000);
}, 280);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
滚动到某个项目并不总是有效,因为 Isotope 使用 CSS 转换进行定位,而不是
top/left
。您可以使用transformsEnabled: false
禁用转换。更好的是,如果您需要使用同位素获取项目的位置,请使用 itemPositionDataEnabled。Scrolling to an item won't always work because Isotope uses CSS transforms for positioning, not
top/left
. You could disable transforms withtransformsEnabled: false
. Better yet, if you need to get the position of an item with Isotope, use itemPositionDataEnabled.我相信我看到了你的问题。尝试用以下代码替换您的
setTimeout
代码:您的原始代码之前无法工作的原因是因为调用
$(document).scrollTo(item, 1000)
时item
尚不可见,因此scrollTo
不知道该项目位于何处。编辑响应代码示例的评论:
看起来您所看到的错误不是由您上面发布的
showBox
函数造成的。该问题是由类showMe
的元素在调用scrollTo
函数后隐藏引起的。尝试在用户单击showMe
元素后立即隐藏它们,而不是使其淡出。I believe I see your problem. Try replacing your
setTimeout
code with this:The reason your original code wasn't working before was because when
$(document).scrollTo(item, 1000)
was calleditem
was not visible yet, soscrollTo
didn't know where the item was located.EDIT in response to comment with the code example:
It looks like the bug you're seeing is not by the
showBox
function you posted above. The problem is caused by elements with the classshowMe
getting hidden after thescrollTo
function has been called. Try hiding theshowMe
elements as soon as the user clicks them instead of fading them out.