Question Background:
I have a 2 page website. Both pages use the same masterlayout.cshtml page which features a Navbar. Within the Navbar is a number of links which scroll down to the relevant divs on page one based on their ID's.
The Issue:
When I access the second page I can no longer redirect to the specified divs from the Navbar links on the first page. This makes sense as the focus is no longer on the first page, but how do I get around this issue?
Code:
Here is the Navbar code with the div id's set, note the data-id
attributes specifying which div to scroll to:
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="#" class="scroll-link" data-id="Welcome">Welcome</a></li>
<li><a href="#" class="scroll-link" data-id="features">Club</a></li>
<li><a href="#" class="scroll-link" data-id="About">About Us</a></li>
<li><a href="#" class="scroll-link" data-id="Location">Location</a></li>
<li><a href='@Url.Action("FutureEvents", "Events", new { pageNo = 1 })'>Events</a></li>
</ul>
</div>
Example of a div that is scrolled to:
<div id="Welcome">
//HTML
</div>
The JQuery used to Scroll to the relevant div:
function scrollToID(id, speed) {
var offSet = 70;
var obj = $(id).offset();
var targetOffset = $(id).offset().top - offSet;
$('html,body').animate({ scrollTop: targetOffset }, speed);
}
If anyone can help with coming up with a viable solution to being able to call these id's from a different page that would be great.
If I understand what you mean. Than you can just do this what an iFrame. In the Navbar for each tab just put a
href='#welcome'
orhref='#features'
and than give the iFrame forwelcome
aID
ofwelcome
. When you click on the tab forwelcome
. You will be send to the iFrame with thewelcome
content. Forfeatures
do the same and all following tabs.You can try something like this. I have changed data-id to id. You can use javascript.
Javascript to scroll
This can be done with cookies. Setting a cookie with
id
you want to scroll, and then, when the new page is loaded, read the cookie and scroll to definedid
. I used the very popular plugin jquery-cookie.Here is the JavaScript code:
Here I have prepared a minimal example with this working:
http://plnkr.co/edit/l2aassM4biyNRWNCrvUz?p=preview
Note: Click on
Events
to nav to the other page.Simplest way is to create another nav bar for other page from where you want to redirect your end users
try this:
create a file first.php or first.html
Then second file with different nav-bar
I guess you may use
$.cookie
every time you scroll on the first page.For Example :
How do you redirect to another page, using
href
value?If so, also add in
data-id
for this<a>
tag too.So when you click on Events link, it will go to another html page and you want to scroll to Events div, right?
Then using
sessionStorage
, you can store and get back the page id, then scroll to its specific div.Add below script and let's see.
JQUERY
HTML (My sample)