ajax 使用锚链接加载内容?实时处理程序?

发布于 2024-10-10 18:51:04 字数 968 浏览 5 评论 0原文

嘿伙计们, 我正在使用 ajax 从子站点加载 div。这个子网站有一个像这样的链接:

<a href="#someIDonHomepage">anchorlink</a>

我的主页,其中触发ajax请求有这个#someIDonHomepage div,我希望我的页面跳转到该位置。

但是,由于我使用 ajax 加载锚点,所以这不起作用。

知道我该如何解决这个问题吗?

我用 jquery 编写了这个动画滚动脚本,当点击锚点时它会产生动画跳转。

/*Animated scroll for anchorlinks*/
    var anchor = '',
        pageOffset = '',
        viewOffset = 30,
        scrollPos = '';
    $(function() {
        $("a[href*='#']").each(function() {
            $(this).addClass('anchorLink');
            $(this).bind('click', function(e) {
                e.preventDefault();
                anchor = $(this).attr('href').split('#')[1];
                pageOffset = $("a[name='#"+anchor+"']").offset();
                scrollPos = pageOffset.top - viewOffset;
                $('html, body').animate({scrollTop:scrollPos}, '500');
            })      
        });
    });

知道我该如何解决这个问题吗?谢谢

hey guys,
i'm loading a div from a subsite with ajax. this subsite has a link in it like this:

<a href="#someIDonHomepage">anchorlink</a>

My homepage where the ajax request is fired has this #someIDonHomepage div and i want my page to jump to that position.

However since i'm loading the anchor with ajax this doesn't work.

any idea how i could solve that?

I wrote this animated scroll script with jquery that animates the jump when clicking on anchors.

/*Animated scroll for anchorlinks*/
    var anchor = '',
        pageOffset = '',
        viewOffset = 30,
        scrollPos = '';
    $(function() {
        $("a[href*='#']").each(function() {
            $(this).addClass('anchorLink');
            $(this).bind('click', function(e) {
                e.preventDefault();
                anchor = $(this).attr('href').split('#')[1];
                pageOffset = $("a[name='#"+anchor+"']").offset();
                scrollPos = pageOffset.top - viewOffset;
                $('html, body').animate({scrollTop:scrollPos}, '500');
            })      
        });
    });

Any idea how i could solve that? thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红墙和绿瓦 2024-10-17 18:51:04

jQuery Ajaxy 内置了对使用 ajax 和哈希的支持,以及对传统锚点的向后支持 - 单击演示中的 durian 选项卡。

http://balupton.com/sandbox/jquery-ajaxy/demo/

jQuery Ajaxy has built in support for using ajax with hashes, as well as backwards support for traditional anchors - click the durian tab in the demo.

http://balupton.com/sandbox/jquery-ajaxy/demo/

暖心男生 2024-10-17 18:51:04

您可以使用 LearningJquery方法甚至直接使用他们的插件。
他们的方法示例

$(document).ready(function() {
  var viewOffset = 30;
  function filterPath(string) {
  return string
 .replace(/^\//,'')
 .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
 .replace(/\/$/,'');
  }
  var locationPath = filterPath(location.pathname);
  var scrollElem = scrollableElement('html', 'body');
  $('a[href*=#]').each(function() {
 var thisPath = filterPath(this.pathname) || locationPath;
 if (  locationPath == thisPath
 && (location.hostname == this.hostname || !this.hostname)
 && this.hash.replace(/#/,'') ) {
   var $target = $(this.hash), target = this.hash;
   if (target) {
  var targetOffset = $target.offset().top - viewOffset;
  $(this).click(function(event) {
    event.preventDefault();
    $(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
   location.hash = target;
    });
  });
   }
 }
  });
  // use the first element that is "scrollable"
  function scrollableElement(els) {
 for (var i = 0, argLength = arguments.length; i <argLength; i++) {
   var el = arguments[i],
    $scrollElement = $(el);
   if ($scrollElement.scrollTop()> 0) {
  return el;
   } else {
  $scrollElement.scrollTop(1);
  var isScrollable = $scrollElement.scrollTop()> 0;
  $scrollElement.scrollTop(0);
  if (isScrollable) {
    return el;
  }
   }
 }
 return [];
  }
});

You can use the LearningJquery approach or even using their plugin directly.
Example of their approach.

$(document).ready(function() {
  var viewOffset = 30;
  function filterPath(string) {
  return string
 .replace(/^\//,'')
 .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
 .replace(/\/$/,'');
  }
  var locationPath = filterPath(location.pathname);
  var scrollElem = scrollableElement('html', 'body');
  $('a[href*=#]').each(function() {
 var thisPath = filterPath(this.pathname) || locationPath;
 if (  locationPath == thisPath
 && (location.hostname == this.hostname || !this.hostname)
 && this.hash.replace(/#/,'') ) {
   var $target = $(this.hash), target = this.hash;
   if (target) {
  var targetOffset = $target.offset().top - viewOffset;
  $(this).click(function(event) {
    event.preventDefault();
    $(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
   location.hash = target;
    });
  });
   }
 }
  });
  // use the first element that is "scrollable"
  function scrollableElement(els) {
 for (var i = 0, argLength = arguments.length; i <argLength; i++) {
   var el = arguments[i],
    $scrollElement = $(el);
   if ($scrollElement.scrollTop()> 0) {
  return el;
   } else {
  $scrollElement.scrollTop(1);
  var isScrollable = $scrollElement.scrollTop()> 0;
  $scrollElement.scrollTop(0);
  if (isScrollable) {
    return el;
  }
   }
 }
 return [];
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文