Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 锚定之间的垂直和水平平滑滚动_Javascript_Html_Scroll_Smoothing - Fatal编程技术网

Javascript 锚定之间的垂直和水平平滑滚动

Javascript 锚定之间的垂直和水平平滑滚动,javascript,html,scroll,smoothing,Javascript,Html,Scroll,Smoothing,我想做平滑滚动,使用锚,在同一页上。 我所有的锚都分布在不同水平或/和垂直水平的页面上。 我在下面得到了这段代码,它只适用于垂直滚动,而不适用于水平滚动。 我应该怎么做才能使滚动同时垂直和水平移动 $(function() { // scroll handler var scrollToAnchor = function( id ) { // grab the element to scroll to based on the name var elem = $("a[name='"+ id

我想做平滑滚动,使用锚,在同一页上。 我所有的锚都分布在不同水平或/和垂直水平的页面上。 我在下面得到了这段代码,它只适用于垂直滚动,而不适用于水平滚动。 我应该怎么做才能使滚动同时垂直和水平移动

$(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 );

}

});

});

您还应该设置scrollLeft属性的动画

$('html, body').animate({
          scrollTop: elem.offset().top,
          scrollLeft: elem.offset().left
}, 1000 );

或者使用jquery插件。

啊,是的,我把它搞糟了:我的代码中有一个输入错误,无法添加左侧,这就是为什么它不起作用。是的,我也在看scrollTo插件。谢谢