简体   繁体   English

JavaScript滚动到锚点

[英]JavaScript scroll to anchor

Having some problems with JavaScript / jQuery. 遇到JavaScript / jQuery的一些问题。

We're currently using a "shortcode" from Visual Composer (WordPress plugin) that creates (horizontal) tabs. 我们目前正在使用Visual Composer(WordPress插件)中的“短代码”创建(水平)标签。 We'we made some custom changes, but only visually (images/css). 我们做了一些自定义更改,但只能在视觉上(images / css)。

We're trying to make it so that when you click on a tab you automatically scroll down to the content. 我们正在努力使其在您单击选项卡时自动向下滚动到内容。

I've tried wrapping the script in 我试过将脚本包装进去

jQuery(document).ready( function({});
jQuery(window).ready( function({});

and also tried replacing .ready() with .load() 并尝试用.load()替换.ready ()

jQuery(".pws_tabs_controlls_item").children().click(function(){
    jQuery('html, body').animate({
         scrollTop: jQuery(".ts-fancy-tabs-container").offset().top
    }, 700, 'swing', function () {});
});

However! 然而! If you copy the entire script and paste it in the Console in Chrome/Firefox it will work as intended, based on that I'd say that the script it self works, it feels like it's not loading or registering or w/e. 如果您复制整个脚本并将其粘贴到Chrome / Firefox中的控制台中,它将按预期工作,基于我说它自己运行的脚本,感觉它没有加载或注册或w / e。

Is there any (obvious) problem/mistake we've made? 我们有没有(明显的)问题/错误?

I'd be happy to provide additional information if neccessary (eg the website address) 如有必要,我很乐意提供更多信息(例如网站地址)

Any help would be greatly appreciated! 任何帮助将不胜感激!

The VisualComposer plugin renders only after the document is ready, and that's why it's not working. VisualComposer插件仅在document准备好后呈现,这就是它无法正常工作的原因。

The best way to solve your problem is by using jQuery's event delegation , eg: 解决问题的最佳方法是使用jQuery的事件委托 ,例如:

jQuery(document).on('click', '.pws_tabs_controlls_item > a', function() {
    jQuery('html, body').animate({
         scrollTop: jQuery(".ts-fancy-tabs-container").offset().top
    }, 700, 'swing', function () {});
});

UPDATE UPDATE

After your comment, I can see that the anchor tags inside your .pws_tabs_controlls_item elements already have a click event listener, and it also uses event.stopPropagation() , so my code above will never be executed. 在您的评论之后,我可以看到.pws_tabs_controlls_item元素中的锚标记已经有一个click事件监听器,它也使用了event.stopPropagation() ,所以我的代码永远不会被执行。

The other way to solve your problem, is by dirty-checking when the element is available, eg: 解决问题的另一种方法是在元素可用时进行脏检查 ,例如:

function waitForAvailability(selector, callback) {
  if (jQuery('selector').length === 0) {
    callback();
  }
  else {
    setTimeout(function() {
      waitForAvailability(selector, callback);
    }, 0);
  }
}

And then, you can use the code you were already using, something like that: 然后,您可以使用您已经使用的代码,类似于:

waitForAvailability('.pws_tabs_controlls_item', function() {
  jQuery(".pws_tabs_controlls_item").children().click(function(){
    jQuery('html, body').animate({
      scrollTop: jQuery(".ts-fancy-tabs-container").offset().top
    }, 700, 'swing', function () {});
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM