I am using the fullPage.js plugin to scroll page by page. The plugin is using hash urls to do so. On page refresh, the document is always loading on the last hash url it was on. How to make the document load at the top after every refresh? I tried adding the following code but it did not work out:
$(document).ready(function(){
$(document).scrollTop(0);
});
This is the link to the webpage - https://rimildeyjsr.github.io/St.Anthony-Website
jQuery :
$('#fullpage #section1').hide();
$(document).ready(function(){
$('#fullpage #section1').show();
$('#fullpage').fullpage({
anchors:['Page1','Page2','lastpage'],
afterLoad : function(anchorLink,index) {
if (index == 2 || anchorLink == 'Page2'){
$('.school-name').addClass('animated slideInUp').css('visibility','visible');
}
}
});
Link to github repository : https://github.com/rimildeyjsr/St.Anthony-Website
It looks like this on your page causes it to go back to the top.
window.location.hash = '#Page1';
So you could try doing that
$(window).on('load', function() {
window.location.hash = '#Page1';
});
which should happen any time you load the page, including refreshing.
Try This
if(location.href.split('#').length > 0){
location.href = location.href.split('#')[0]
}
Let me know if you face any issue
The proper way would be by using the fullPage.js function moveTo:
$(window).on('load', function() {
$.fn.fullpage.moveTo(1);
});
But the suggested solution by changing the location hash will also do it.
if (location.hash) {
setTimeout(function() {
window.scrollTo(0, 0);
}, 1);
}
Related
I have a onepager site where I use scrollmagic plus all its necessary plugins/libraries (and jQuery) for different effects where animation, pinning, fading processes etc. are triggered by scroll positions.
I also use it for animated scrolling to the anchor points on the page (from the menu and from other local links) - see the according part of the script below.
The problem is that this script suppresses the default behaviour of "jumping" directly to an anchorpoint when a local link is clicked, and apparently also when the page is accessed from outside via a direct link or bookmark with an anchor appended to the URL (like http://www.example.com/index.php#part3). Altough this behaviour is desired when clicking a local link, it obviously prevents the browser from displaying the anchor position when an anchor is linked from somewhere else.
Is there any way to make the browser directly display that anchor position when a link like in the above example is clicked?
var sm_controller_1 = new ScrollMagic.Controller();
sm_controller_1.scrollTo(function(anchor_id) {
TweenMax.to(window, 2.0, {
scrollTo: {
y: anchor_id
autoKill: true
},
ease: Cubic.easeInOut
});
});
jQuery(document).on("click", "a[href^=#]", function(e) {
var id = jQuery(this).attr('href');
if(jQuery(id).length > 0) {
e.preventDefault();
sm_controller_1.scrollTo(id);
if (window.history && window.history.pushState) {
history.pushState("", document.title, id);
}
}
});
(It doesn't make sense to create a fiddle/codepen since the problem lies in calling the original URL from an external source).
Well assuming scroll magic doesnt have extra functionality that is not posted here that would get in the way of my answer you could try this:
Add a data-attribute to your links which you want to use default behavior:
<a href="example.com/index.php#part3.php" data-default="true">
Check if that data attribute exists and if it does return true in your click handler to continue with the default behavior:
var sm_controller_1 = new ScrollMagic.Controller();
sm_controller_1.scrollTo(function(anchor_id) {
TweenMax.to(window, 2.0, {
scrollTo: {
y: anchor_id
autoKill: true
},
ease: Cubic.easeInOut
});
});
jQuery(document).on("click", "a[href^=#]", function(e) {
if(e.currentTarget.dataset.default){
return true;
}
var id = jQuery(this).attr('href');
if(jQuery(id).length > 0) {
e.preventDefault();
sm_controller_1.scrollTo(id);
if (window.history && window.history.pushState) {
history.pushState("", document.title, id);
}
}
});
You can try and use this code:
jQuery(function ($) {
$(document).ready(function() {// if needed, use window.onload which fires after this event
if(window.location.hash) {
var hash = window.location.hash;
$( 'a[href=' + hash + ']' ).click();
}
});
});
It will wait till the DOM (or the page) is loaded and then simulate the user click on the nav item.
After I've read your question one more time, I am not sure anymore if you want your page loaded on the position of the element which is listed in the anchor or the scroll wasn't working when coming from an external source?
If the scroll was working but you wanted to display the page at the right place, like a jump, then I propose 2 solutions:
a) use the CSS opacity:0; on the body and after the scroll is finished, set it back to opacity:1;
b) try to jump on the proper place on the page before you load ScrollMagic
I have tried to get this to work and was unsuccessful. I just want to have my mobile page views load at the top. I have tried the following.
// Method 1:
$('body').scrollTop(0);
// Method 2:
document.body.scrollTop = 0;
// Method 3:
$(window).on('beforeunload', function(){
$(window).scrollTop(0);
});
I tried here first and was unsuccessful.
I just want to always start at the top of the page when I click a link in mobile. I have no idea as to why none of these solutions work.
You can use also vanilla JS for it:
document.body.scrollTop = document.documentElement.scrollTop = 0;
or with JQuery
$(document).ready(function() {
$(this).scrollTop(0);
});
Try also
$('html,body').scrollTop(0);
Check this demo as well, might help
https://jsfiddle.net/a_incarnati/v575uvyb/2/
You should include $(this).scrollTop(0); within a $(document).ready(); call, like so:
$(document).ready(function() {
$(this).scrollTop(0);
});
In your code, you are calling $(this).scrollTop(0); before the page unloads, rather than after the page and DOM have been reloaded.
I'm using the following script with jQuery to have a fade transition when navigating between pages:
$(document).ready(function() {
$("body").css("display", "none");
$("body").fadeIn(200);
$("a").click(function(event){
event.preventDefault();
linkLocation = this.href;
$("body").fadeOut(200, redirectPage);
});
function redirectPage() {
window.location = linkLocation;
}
});
It works fine when only using links to navigate between pages, but when using the back button on the browser, the page returned to is blank.
How can I get pages to be properly displayed when navigating to them via the back button?
Using the back button returns you to the state of the previous page right before you left it (in this case, completely faded out), at least that's how I understand it. Feel free to correct me if I'm wrong though.
In any case, I think repainting the DOM would solve your problem (taken from Coderwall):
$.fn.redraw = function(){
$(document).each(function(){
var redraw = this.offsetHeight;
});
};
And to call the function: $(document).redraw();
How about try using delegate
$("a").on('click', function(event){
event.preventDefault();
linkLocation = this.href;
$("body").fadeOut(200, redirectPage);
});
I need to run some functions on initial page load and also if the page is refreshed, if the url contains a particular hash.
As of now, I am able to get it working only on page refresh and not working when i first get to the page.
My code:
$(document).ready(function() {
if (document.location.href.indexOf('#story') > -1) {
alert('true');
}
});
You should be working with window.location.hash like this:
$(document).ready(function() {
if (window.location.hash === '#story') {
alert('true');
}
});
I've 3 divs (#Mask #Intro #Container) so if you click on Mask, Intro gets hidden and Container appears.
The problem is that I just want to load this only one time, not every time I refresh the page or anytime I click on the menu or a link, etc.
How can I do this?
This is the script I'm using for now:
$(document).ready(function(){
$("div#mask").click(function() {
$("div#intro").fadeToggle('slow');
$("div#container").fadeToggle('slow');
$("div#mask").css("z-index", "-99");
});
});
Thank you!
You can try using a simple counter.
// count how many times click event is triggered
var eventsFired = 0;
$(document).ready(function(){
$("div#mask").click(function() {
if (eventsFired == 0) {
$("div#intro").fadeToggle('slow');
$("div#container").fadeToggle('slow');
$("div#mask").css("z-index", "-99");
eventsFired++; // <-- now equals 1, won't fire again until reload
}
});
});
To persist this you will need to set a cookie. (e.g. $.cookie() if you use that plugin).
// example using $.cookie plugin
var eventsFired = ($.cookie('eventsFired') != null)
? $.cookie('eventsFired')
: 0;
$(document).ready(function(){
$("div#mask").click(function() {
if (eventsFired == 0) {
$("div#intro").fadeToggle('slow');
$("div#container").fadeToggle('slow');
$("div#mask").css("z-index", "-99");
eventsFired++; // <-- now equals 1, won't fire again until reload
$.cookie('eventsFired', eventsFired);
}
});
});
To delete the cookie later on:
$.cookie('eventsFired', null);
Just point to an empty function once it has been called.
var myFunc = function(){
myFunc = function(){}; // kill it
console.log('Done once!'); // your stuff here
};
Web pages are stateless in that they don't hold states between page refreshes. When you reload the page it has no clue what has happened in the past.
Cookies to the rescue! You can use Javascript (and jQuery has some nice plugins to make it easier) to store variables on the client's browser. Store a cookie when the mask is clicked, so that when the page is next loaded it never shows.
this code with will work perfect for you and it is the standard way provided by jquery to bind events that you want to execute only once
$(document).ready(function(){
$("div#mask").one('click', function() {
$("div#intro").fadeToggle('slow');
$("div#container").fadeToggle('slow');
$("div#mask").css("z-index", "-99");
});
});