简体   繁体   English

单击锚点执行javascript函数,但代码必须在该锚点打开的页面上运行

[英]Execute javascript function on anchor click, but the code must run on the page opened by that anchor

Is this behaviour possible? 这可能吗? I would need the code that is inside on click event handler, to be executed on the new HTML page that is opened by that navigation link. 我需要单击事件处理程序中的代码,才能在该导航链接打开的新HTML页面上执行。

I've put up a small fiddle to better illustrate what I want to achieve. 我提出了一个小提琴,以更好地说明我想要实现的目标。 http://jsfiddle.net/hWEpL/4/ http://jsfiddle.net/hWEpL/4/

Here is the jQuery for scrolling: 这是用于滚动的jQuery:

        $.fn.scrollView = function () {
            return this.each(function () {
                $('html, body').animate({
                    scrollTop: $(this).offset().top
                }, 1000);
            });
        }

        $('#link-3').click(function (event) {
            event.preventDefault();
            $('.section-3').scrollView();
        });

Let's assume that the fiddle is Home Page. 让我们假设小提琴是主页。 When clicking on Section-3, the page scrolls to section-3. 单击第3节时,页面滚动到第3节。

Now, let's say the we are on the second page(about) and we click on Section-3. 现在,假设我们在第二页(关于),然后单击第3节。 I would like to have Home Page opened, and that the page is scrolled to section-3. 我想打开主页,并将页面滚动到第3节。

The thing is that I don't want this behaviour when the Home Page is opened, I only need it when someone is on the other pages and clicks on Section-3 事实是,当打开主页时,我不希望出现这种情况,只有在其他页面上有人并单击第3节时才需要它

You can use Fragment identifiers . 您可以使用片段标识符 So in the about page for example, you would link to the home page with homepage.htm#section-3 and in homepage.htm, you would include just like the code you have included but add 因此,例如在About页面中,您将使用homepage.htm#section-3链接到主页,而在homepage.htm中,您将包含与所包含的代码相同的内容,但是添加

var hash=location.hash
if(hash){
var el=$('.'+hash.substr(1))
  if(el)
    el.scrollView();
}

in the onLoad. 在onLoad中。 Here we use the fragment as a way to tell the page which element to go to. 在这里,我们使用片段作为告诉页面要转到哪个元素的方法。

http://jsfiddle.net/hWEpL/5/ This is the updated jsfiddle (with simulated hashed location being already followed using http://jsfiddle.net/hWEpL/5/这是更新的jsfiddle(使用来跟踪模拟的哈希位置)

location.href="#section-3" //simulate named links . location.href="#section-3" //simulate named links do not include this in your code 不要将此包含在您的代码中

Moving forward, perhaps semantically, you could use ID instead of class, to use the default behaviour of fragments if javascript is disabled, but then you have to place this piece of code to actually prevent the default behaviour (and allow the animation to occur) as stated in https://stackoverflow.com/a/3659116/1480215 向前移动,也许从语义上讲,如果禁用了javascript,则可以使用ID而不是类来使用片段的默认行为,但是您必须放置这段代码以实际防止默认行为(并允许动画发生)如https://stackoverflow.com/a/3659116/1480215中所述

setTimeout(function() {
  if (location.hash) {
    window.scrollTo(0, 0);
  }
}, 1);

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

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