Jquery 跳转到X方向的ID标记
我现在正在使用这个来跳转到页面上的id标签,虽然我需要能够跳转到X方向,如果可能的话,那会很好吗 因此,基本上我只需要一个按钮,它将滚动到一个div,该div位于页面的x方向,与垂直方向相同:)Jquery 跳转到X方向的ID标记,jquery,scroll,Jquery,Scroll,我现在正在使用这个来跳转到页面上的id标签,虽然我需要能够跳转到X方向,如果可能的话,那会很好吗 因此,基本上我只需要一个按钮,它将滚动到一个div,该div位于页面的x方向,与垂直方向相同:) 我希望我理解你的问题:) 如果您只想向上或向左滚动,请在此处删除另一个ptoperty表单 $('html,body').animate({scrollTop:$target.offset().top,scrollLeft:$target.offset().left},600) 以下是一种似乎有效的方
我希望我理解你的问题:) 如果您只想向上或向左滚动,请在此处删除另一个ptoperty表单
$('html,body').animate({scrollTop:$target.offset().top,scrollLeft:$target.offset().left},600) 以下是一种似乎有效的方法:
$('a').click(
function(e){
// prevents the default 'jump'
e.preventDefault();
// caches the href of the clicked link
var href = this.href,
// takes the href, splits off the '#' character (and anything before it)
// and uses that to find the id of the target element.
target = $('#' + href.substring(href.indexOf('#') + 1)),
// finds, and caches the offsets
offsets = target.offset(),
// finds the offset of the top of the element (relative to the page)
top = offsets.top,
left = offsets.left;
// animates the movement to the top, and the left, position of the target
$('body').stop().animate({
scrollTop : top,
scrollLeft : left
}, 1000);
});
修改了上述内容,使用split()
代替substring()
:
而且,如果您希望有两个不同的滚动事件,此版本将有助于:
$('a').click(
function(e) {
e.preventDefault();
var target = $('#' + this.href.split('#')[1]),
offsets = target.offset(),
top = offsets.top,
left = offsets.left;
$('body').stop().animate({
scrollTop: top
}, 1000, function() {
$(this).animate({
scrollLeft: left
}, 1000);
});
});
.我不这么认为:(我基本上想要这个效果:但在x方向而不是y方向:(编辑了我的帖子。这就是你要找的吗?那就是:)()()()()()))但是有没有一种方法可以让它同时做这两件事呢?编辑帖子,这样你就可以看到如何只向上或向左滚动。如果它很酷,请投它一票:)我,伙计们,你们都做了我所描述的,但有没有办法让它从上到下同时动画?不向左滚动,然后向下滚动?你应该删除小提琴顶部ul id属性中的哈希。哦,亲爱的上帝……我不敢相信我没有看到><谢谢!(并经过编辑!以纠正失明。可能还有愚蠢。叹气。)
//anchors
function filterPath(string) {
return string
.replace(/^\//,'')
.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
.replace(/\/$/,'');
}
$('a[href*=#]').each(function() {
if ( filterPath(location.pathname) == filterPath(this.pathname)
&& location.hostname == this.hostname
&& this.hash.replace(/#/,'') ) {
var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) +']');
var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
if ($target) {
$(this).click(function() {
//Use offset top and left to go to the divs position
$('html, body').animate({scrollTop: $target.offset().top, scrollLeft: $target.offset().left}, 600);
return false;
});
}
}
});
$('a').click(
function(e){
// prevents the default 'jump'
e.preventDefault();
// caches the href of the clicked link
var href = this.href,
// takes the href, splits off the '#' character (and anything before it)
// and uses that to find the id of the target element.
target = $('#' + href.substring(href.indexOf('#') + 1)),
// finds, and caches the offsets
offsets = target.offset(),
// finds the offset of the top of the element (relative to the page)
top = offsets.top,
left = offsets.left;
// animates the movement to the top, and the left, position of the target
$('body').stop().animate({
scrollTop : top,
scrollLeft : left
}, 1000);
});
$('a').click(
function(e){
e.preventDefault();
var href = this.href,
// as above, but more concise
target = $('#' + href.split('#')[1]),
offsets = target.offset(),
top = offsets.top,
left = offsets.left;
$('body').stop().animate({
scrollTop : top,
scrollLeft : left
}, 1000);
});
$('a').click(
function(e) {
e.preventDefault();
var target = $('#' + this.href.split('#')[1]),
offsets = target.offset(),
top = offsets.top,
left = offsets.left;
$('body').stop().animate({
scrollTop: top
}, 1000, function() {
$(this).animate({
scrollLeft: left
}, 1000);
});
});