How can I remove the location hash without causing the page to scroll?

后端 未结 8 1581
情深已故
情深已故 2020-12-04 08:18

Is it possible to remove the hash from window.location without causing the page to jump-scroll to the top? I need to be able to modify the hash without causing

8条回答
  •  醉话见心
    2020-12-04 08:52

    Hope this helps

    html

    
    

    1. Content goes here

    2. Content goes here

    3. Content goes here

    js

    function tabs(){
      $(".content").hide();
    
      if (location.hash !== "") {
        $('.tabs ul li:has(a[href="' + location.hash + '"])').addClass("active");
        var hash = window.location.hash.substr(1);
        var contentClass = "." + hash;
        $(contentClass).fadeIn();
      } else {
        $(".tabs ul li").first().addClass("active");
        $('.tabs').next().css("display", "block");
      }
    }
    tabs();
    
    $(".tabs ul li").click(function(e) {
      $(".tabs ul li").removeAttr("class");
      $(this).addClass("active");
      $(".content").hide();
      var contentClass = "." + $(this).find("a").attr("href").substr(1);
      $(contentClass).fadeIn();
      window.location.hash = $(this).find("a").attr("href");
      e.preventDefault();
      return false;
    });
    

    URL without any hash.
    http://output.jsbin.com/tojeja

    URL with hashtag that does not jumping to anchor.
    http://output.jsbin.com/tojeja#content1

提交回复
热议问题