簡體   English   中英

相同和不同頁面的錨點鏈接

[英]Anchor link for same and different page

我正在使用該代碼:

$(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;
      }
    }
  });
});

<a href="#allcontentcontact">鏈接到div <div id="allcontentcontact"></div>

問題是:“#allcontentcontact”在我的主頁上,所以只有當我在主頁上時,代碼才能完美運行。 但是我有一個固定的菜單鏈接到其他頁面! 因此,當我在另一個頁面上時,我需要一些能夠使我的href鏈接到該div的東西。

謝謝!

謝謝你的答案,我做到了! :)

<script>
var url = "http://192.168.1.157/wp/";
$(function(){
  if (location.href==url){
    $('#menu #mylink').attr("href","#bluecontent");
    $('#menu #mylink2').attr("href","#allcontentcontact");
  }
});
</script>

URL很奇怪,因為我在localhost上編碼,但代碼運行完美。 :)

和HTML

<a id="mylink" href="<?php bloginfo('url'); ?>#bluecontent"><span>Parceiros</span></a>

<a id="mylink2" href="<?php bloginfo('url'); ?>#allcontentcontact"><span>Contato</span></a>

它差不多完成了。 所有鏈接都能很好地完美地帶我到他們指定的錨點。

問題是:我的腳本滑動到其他頁面上的錨點,包括URL上的#bluecontent和#allcontentcontact。 是否可以從URL中刪除它? 這是代碼:

var jump=function(e)
{
   if (e){
       e.preventDefault();
       var target = $(this).attr("href");
   }else{
       var target = location.hash;
   }

   $('html,body').animate(
   {
       scrollTop: $(target).offset().top
   },2000,function()
   {
       location.hash = target;
   });

}

$('html, body').hide();

$(document).ready(function()
{
    $('a[href^=#]').bind("click", jump);

    if (location.hash){
        setTimeout(function(){
            $('html, body').scrollTop(0).show();
            jump();
        }, 0);
    }else{
        $('html, body').show();
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM