简体   繁体   English

锚标签链接到具有动画的不同页面上的ID

[英]Anchor tag link to ID on different page with animate

I have 2 page website, im trying to link second page back to first page where the div is present im trying with. 我有2页的网站,我正在尝试将第二页链接回div所在的第一页。

<li class="menuitem">
    <a href="index.html#services">Go Back</a>
</li>

It goes to the div with id tag services and leaves the id tag on address bar. 它通过id标签服务转到div,并将id标签留在地址栏上。

Im trying to animate it to the place and remove id tag from the address bar. 我正在尝试将其设置为动画并从地址栏中删除id标签。

this is how page 1 looks like - 这就是第1页的样子-

<div id="home"></div>
<div id="about"></div>
<div id="gallery"></div>
<div id="services"></div>
<div id="contact"></div>

jquery used for linking div in same page - 用于在同一页面中链接div的jquery-

$(function() {
    $('a[href*=#]:not([href=#])').click(function() {
        if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
            var target = $(this.hash);
            target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
            if (target.length) {
                $('html,body').animate({
                    scrollTop: target.offset().top
                }, 1000);
                return false;
            }
        }
    });
});

with window.location.hash you can read and write the hash-value. 使用window.location.hash可以读取和写入哈希值。 So on pageload you do basically the following: 因此,在pageload上,您基本上可以执行以下操作:

$("html, body").animate({ 
   scrollTop: $('#'+window.location.hash).offset().top 
}, 1000);
window.location.hash='';

You can not modify URL after it loaded you have to play with your request 加载URL后,您不能再修改它,您必须处理您的请求

http://api.jquery.com/focus/ http://api.jquery.com/focus/

Try using cookie storing and then using jquery you can focus on that one 尝试使用Cookie存储,然后使用jquery,您可以专注于那一个

Or using javscript 或者使用javscript

window.onload=getloaded();
function getloaded() {    
      var g=document.URL ;
      if(g.indexOf('services')!=-1) {
          // focus on services
      }
}

or like this 或像这样

$('html,body').animate({scrollTop:$("#your element").offset().top},'slow');

Simply use anchor tag with 只需将锚标签与

href="my_page.html#my_anchor_name"

which will scroll to any html element with id equal to my_anchor_name in the page my_page.html provided the page belongs to the same domain. 如果页面属于同一域,它将滚动到页面my_page.html id等于my_anchor_name任何html元素。 If you want to link within the same page, simply omit the page name my_page.html . 如果要在同一页面内链接,只需省略页面名称my_page.html

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

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