简体   繁体   English

在同一窗口(标签)中打开链接之前,请先转到页面顶部。 如何?

[英]Go to top of the page before opening link in the same window(tab). How to?

I want to go to top of the page when click on link and then open link in the same tab 单击链接然后在同一选项卡中打开链接时,我想转到页面顶部

I tried to use this code, but I only go to top of the page and link doesnt open 我尝试使用此代码,但是我仅转到页面顶部,并且链接未打开

This is my JS 这是我的JS

$(document).ready(function(){
$('.scrollToTop').click(function(){
        $('html, body').animate({scrollTop : 0},1800);
        return false;
    });
});

And this is my HTML link 这是我的HTML链接

<a href="./current_page?page=5" class="scrollToTop">&gt;</a>

Please help) 请帮忙)

Try this 尝试这个

$(document).ready(function(){
    $('.scrollToTop').click(function(){
        const url = $(this).attr('href');
        $('html, body').animate({scrollTop : 0}, 1800, function() {
            window.open(url);
        });
        return false;
    });
});

One way to achieve this is to execute page navigation via JavaScript, after the scroll to top animation has completed. 实现此目的的一种方法是在滚动到顶部动画之后,通过JavaScript执行页面导航。

This can be done by navigating the broswer during the .promise().done( .. ) callback, where you'd update the location to the href of the clicked link: 这可以通过在.promise().done( .. )回调期间浏览浏览器来完成,在该回调中,您将location更新为单击链接的href:

 $(document).ready(function() { $('.scrollToTop').click(function() { /* Obtain href from clicked link element */ const href = $(this).attr('href'); $('html, body').animate({ scrollTop: 0 }, 1800) .promise() .done(function() { /* After animation complete, navigate to link's href */ location = href; console.log(`Navigate: ${href} (won't work in stack overflow sandbox)`); }); return false; }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <h1>Scroll past</h1> <h1>Scroll past</h1> <h1>Scroll past</h1> <h1>Scroll past</h1> <a href="./current_page?page=5" class="scrollToTop">Scroll to top, then navigate &gt;</a> 

Since your click event Is returning false the href tag of your anchor won't redirect. 由于您的click事件返回false ,因此您锚点的href标记将不会重定向。

Note that since you are changing routes through an anchor the loaded page will always start at the top of the body. 请注意,由于您正在更改通过锚点的路线,因此加载的页面将始终从正文的顶部开始。

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

相关问题 在新窗口/标签页中打开链接后,如何向页面添加内容? - How can I add content to a page after opening the link in a new window/tab? 点击链接后如何go到页面顶部? - How to go to the top of the page after clicking the link? window.open(url)在同一选项卡中未打开新网页 - window.open(url) not opening new web page in the same tab 在同一模态窗口中打开一个链接 - Opening a link in the same modal window 如果选中了复选框,则在新选项卡中打开链接,否则在同一页面上打开链接 - Opening link in new tab if Check box is checked Otherwise opening it on same page 在父窗口的新标签页中打开iframe链接 - Opening an Iframe link in a new tab on the parent window 防止在同一选项卡中打开链接,但允许在新选项卡中打开 - Prevent opening link in same tab, but allow opening in a new tab 如何点击链接上的链接和呼叫功能同时进行链接? - How to click on a link and call function on the page that link go to as the same time? 如何在新窗口中而不是同一窗口及其新选项卡中打开页面? - How to open page in new window and not in same window and on its new tab? 我的链接没有打开它应该打开的页面,但会在新标签中打开 - My link is not opening the page it should but will in a new tab
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM