首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >锚固间垂直和水平平滑滚动

问锚固间垂直和水平平滑滚动
EN

Stack Overflow用户
提问于 2014-04-06 20:18:03
回答 1查看 1.8K关注 0票数 0

我想做光滑的滚动,使用锚,在同一页。我所有的锚都在不同的水平或/和垂直水平上展开在一个页面上。下面是这段代码,它只适用于垂直滚动,而不适用于水平滚动。我应该做些什么,使滚动在垂直和水平同时进行?

代码语言:javascript
复制
$(function() {

// scroll handler
var scrollToAnchor = function( id ) {

// grab the element to scroll to based on the name
var elem = $("a[name='"+ id +"']");

// if that didn't work, look for an element with our ID
if ( typeof( elem.offset() ) === "undefined" ) {
  elem = $("#"+id);
}

// if the destination element exists
if ( typeof( elem.offset() ) !== "undefined" ) {

  // do the scroll
  $('html, body').animate({
          scrollTop: elem.offset().top
  }, 1000 );

}
};

// bind to click event
$("a").click(function( event ) {

// only do this if it's an anchor link
if ( $(this).attr("href").match("#") ) {

  // cancel default event propagation
  event.preventDefault();

  // scroll to the location
  var href = $(this).attr('href').replace('#', '')
  scrollToAnchor( href );

}

});

});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-06 20:41:39

你也应该激活scrollLeft属性.

代码语言:javascript
复制
$('html, body').animate({
          scrollTop: elem.offset().top,
          scrollLeft: elem.offset().left
}, 1000 );

或者使用jquery scrollTo插件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22899471

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档