简体   繁体   English

导航到div而不更改url

[英]Navigate to div without changing url

I am building a single page web application where I don't the require the url changing when clicking on the anchor tag that is linked to div in same page. 我正在构建一个单页Web应用程序,在单击链接到同一页面中div的锚标记时,不需要更改URL。 div id gets added to the url. div ID被添加到网址中。 Kindly help in how to navigate to the div without changing the url. 请在不更改网址的情况下帮助您导航到div。

This is my code so far: 到目前为止,这是我的代码:

 $(document).ready(function () { $('a').on("click",function(e){ window.location="#link5" e.preventDefault(); e.stopPropagation(); console.log(window.location); }); }) 
 #login { position: absolute; top: 50%; left: 50%; } a { text-decoration: none; display: inline-block; padding-right: 15px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <a href="#">Link1</a> <a href="#">Link2</a> <a href="#">Link3</a> <a href="#">Link4</a> <a href="#">Link5</a> <h4>Content of the above links</h4> <div id="link1"> <h3>Contents of #link1</h3> <p>Sample contents placed in the div #link1</p> <p>Sample contents placed in the div #link1</p> <p>Sample contents placed in the div #link1</p> </div> <div id="link2"> <h3>Contents of #link2</h3> <p>Sample contents placed in the div #link2</p> <p>Sample contents placed in the div #link2</p> <p>Sample contents placed in the div #link2</p> </div> <div id="link3"> <h3>Contents of #link3</h3> <p>Sample contents placed in the div #link3</p> <p>Sample contents placed in the div #link3</p> <p>Sample contents placed in the div #link3</p> </div> <div id="link4"> <h3>Contents of #link4</h3> <p>Sample contents placed in the div #link4</p> <p>Sample contents placed in the div #link4</p> <p>Sample contents placed in the div #link4</p> <p>Sample contents placed in the div #link4</p> </div> <div id="link5"> <h3>Contents of #link5</h3> <p>Sample contents placed in the div #link5</p> <p>Sample contents placed in the div #link5</p> <p>Sample contents placed in the div #link5</p> <p>Sample contents placed in the div #link5</p> </div> 

http://jsfiddle.net/vkarthikcse/3duv3ozz/3/ http://jsfiddle.net/vkarthikcse/3duv3ozz/3/

$ http://jsfiddle.net/xovd9c8t/2/ $ http://jsfiddle.net/xovd9c8t/2/

with animation, using modern data- elements 动画,使用现代数据元素

This will give you a smooth transition while navigating to that part of the page. 这将使您在导航到页面的那部分时平稳过渡。 User experience will be amazing. 用户体验将是惊人的。

This should work: http://jsfiddle.net/3duv3ozz/4/ 这应该工作: http : //jsfiddle.net/3duv3ozz/4/

Of course right now it's hard coded to only work with one link, but you should get the idea: 当然,现在仅使用一个链接很难进行编码,但是您应该了解一下:

$(document).ready(function () {
    $('a').on("click",function(e){

        var posTop = $("#link5").offset().top; //Get the position of the element you want to scroll to
        e.preventDefault();
        e.stopPropagation();
        $('html, body').scrollTop(posTop); //Scroll to that position
        console.log(window.location);

    });
})

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

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