Navbar Not Switching To Fixed When I Scroll By It - javascript

For reference here is the url http://buildme.co/
I am currently having a issue with my main navbar not switching to fixed as you scroll by it. This is the navbar under the skewed images.
The Following JavaScript code is supposed to be making it do this
// Change To Fixed Header
$(document).ready(function(){
$(window).bind('scroll', function() {
var navHeight = $( window ).height() - 100;
if ($(window).scrollTop() > navHeight) {
$('.main-navbar').addClass('navbar-fixed-top');
}
else {
$('.main-navbar').removeClass('navbar-fixed-top');
}
});
});
However this is not the case and nothing happens.
Here is a example of what I am trying to achieve. http://stanislav.it/tutorials/sticky-navigation/

It looks like you may need to bind the scroll event to the 'body' in your case (the window scroll event wasn't firing when I checked it on your site but the body was)
Edit: Oh wait I think I see...It's the overflow styling you've added to html, body:
html, body {
overflow-x: hidden;
height: 100%;
}
if I get rid of the overflow-x: hidden your code works for me. I know I've had trouble before with the fact that it's not allowed to have one axis allow overflow and the other not (so if you set overflow-x: hidden it may do funny things with the y scrolling)

Related

CSS Fixed element with input focus fixture but preventing animate scrolltop

I have a fixed box at the footer of a product page, which is working fine now with this fix applied:
html, body {
-webkit-overflow-scrolling : touch !important;
overflow: auto !important;
height: 100%;
}
As it fixed the problem on iPhone where you focused in the QTY input field and it pulled it down to the bottom of the page; that fix above now sorted that and works perfect!
However, the accordion tabs on the page dont work a expected, if I remove that fix for the other issue they work but with they don't.
I think it might be easier to do a fix for the scrollto tab as I have searched and read about the fix focus issue; what it does when you click a tab now is just goes to the top of the page and does not scroll to the active tab. Here is the function:
function scrollToTab(x) {
$("html, body").animate({
scrollTop: $(x).offset().top - 10
}, scrollSpeed);
}
I am sure it is the html, body its trying to animate and scroll so anyone know what I can maybe change to this to get both working?
Maybe only set those styles / fix on html, body when input focused and remove when not? that might work.

jQuery mobile scrolling/page cut off issue on multi page template

Having this strange issue where page/screen is being cut off.
Seems to happen when you click into this screen below (which is shorter) then click back and go into another longer screen. The scrollbar doesn't appear so the page is cut off. Testing in Chrome v36 & iOS Safari.
http://onegreenthing.net/sunsuper/RetirementModeller/Forecast.html#retirement-age
Any ideas?
PS: Resize window so it's mobile like width (e.g. 400px wide)
.the-thing:before {
content: "";
display: block;
height: 0;
padding-top: 56.25%;
}
Smells like psuedo-vertical-centering that isn't recalculated to vanish appropriately if the page's content is naturally taller than the viewport.
Post-comment update - try this:
if( $(document).height() > $(window).height() ) {
$(".the-thing").remove();
}
Post-comment#2 - your main container element has this css (for id='retirement-age'):
.ui-mobile .ui-page {
min-height: 300px;
}
if I pull up my window shorter than 300 - I get the no-scroll thing - not buggy - that's correct.
Ok rookie error :( ..there was page with duplicate id, removed it and fixed the issue.

Using scrollTop inside of a fixed element with overflow-y:scroll

I can't seem to get scrolltop to work when my content is inside of a fixed position container that has overflow-y: scroll specified.
Here is my relevant code:
/* some container with many large content blocks inside */
#container {
position: fixed;
width: 300px;
height: 300px;
overflow-y: scroll;
}
/* button that has a data-path attribute that specifies where the container should scroll to*/
$(".button").on("click", function(){
var path = $(this).attr("data-path");
var anchor = $("#" + path);
var position = anchor.position().top;
$("#container").animate({scrollTop: position});
});
I believe this fiddle illustrates my dilemma quite well: http://jsfiddle.net/Qndu5/
If you scroll from the top down to an element, it works great. After that, all bets are off. It is completely incapable of scrolling from any position other than the top. It either horribly misses the mark, or scrolls all the way back to the top, even though the position values being fed to it are seemingly correct.
Surely I'm missing something here, but I'm not sure what I am not understanding. Thanks for any help provided!
What you are missing is the scrollTop when calculating the position, so if the view is already scrolled, that need to be added to the calculation var position = anchor.position().top + $("#container").scrollTop();
http://jsfiddle.net/x36Rm/

Unexplained jQuery bug with scroll animation

I have a small problem with a short jQuery script.
I would like to create a simple scroll system, between 100% height section on my page.
I tried it, but there's a strange problem when using animate() jQuery function.
I've uploaded files on a server : http://www.fitnessmith.fr/test/
Try navigating through sections with links on right, and you'll see what I mean.
Here's the code used to create animations
$("nav#menu li a").on("click", function(e){
e.preventDefault();
var href = $(this).attr("href");
$('html, body').animate({scrollTop:$(href).offset().top}, 600, 'easeInExpo');
});
Does anyone has an idea to solve it ? thanks for your help
The problem is that you are applying overflow: hidden to both html and body elements. If you remove the overflow from your html,body rule, only the document body element will be overflow:hidden by virtue of your specific body rule.
The reason why this happened is that when you animated the scrollTop property of both html and body, due to neither having overflow and thus unable to truly scroll, what changed was the content of body relative to their offset parent.
You can see this in action if you called $('#about').offset() after clicking the link once. It reports a top position of 0! What has happened is that the "top" of your page has simply moved up in order to scroll the content to the viewport while maintaining no overflow. $('#home').offset() reports a large negative top at that point to indicate this.
By removing the html,body { overflow: hidden; } rule, you are allowing the content to be scrolled against a container while the more specific body { overflow: hidden; } rule hides the scrollbars.

Setting overflow-y: hidden; causes the page to jump to the top in Firefox

I've got some javascript which handles opening modal popups on my website, and it also sets the overflow-y property on the <html> element to hidden. In Chrome and IE this works as expected - the scrollbar hides, and the page behind the modal popup remains in the same scroll position. When the popup is closed, overflow-y is set to scroll and the page is in the same state and position as before.
However in Firefox, as soon as overflow-y is changed to hidden the page scroll position jumps to the very top, and so when the popup is closed the view has changed for the user - not ideal.
The problem can be seen on this jsfiddle
Is there any solution for this behaviour?
Don't use overflow: hidden on html, only on body.
I had the same problem but fixed it by removing html.
Instead :
$('body, html').css('overflow', 'hidden');
Do :
$('body').css('overflow', 'hidden');
I had the same issue
after checking it in the inspector window, I noticed that in the reset CSS, HTML is set to
HTML {
overflow-y: scroll;
}
you can fix this by setting it to
HTML {
overflow-y: initial;
}
If you don't want to touch reset CSS or just comment it
plugin and code is absolutely fine
change modal position from absolute to fixed:
#mymodal {
position: fixed
}
There are lots of bugs in the different browsers and the functionality is all over the place so be careful modifying styles on body and html tags.
To solve this issue i had to wrap the body's content into its own element and apply the scrolling restriction on it:
var $content = $('<div/>').append($body.contents()).appendTo($body);
$content.css('overflow-y', 'hidden');
This is the only way i've been able to get this working consistently across different browsers and devices.
I just encountered this problem. My fix was
/**
* Store the scroll top position as applying overflow:hidden to the body makes it jump to 0
* #type int
*/
var scrollTop;
$(selecor).unbind('click.openmenu').on('click.openmenu', function (e) {
// Stuff...
scrollTop = $('body').scrollTop() || $('html').scrollTop();
$('body,html').css({overflow: 'hidden'});
});
$(selector).unbind('click.closemenu').on('click.closemenu', function (e) {
// Stuff
$('body,html').css({overflow: 'initial'}).scrollTop(scrollTop);
});
This however doesn't solve the problem of what happens if a user resize the viewport.
Edit: I just saw your code and you used a link with href="#". That is most likely the cause. I'd suggest removing the href property or use a button for it.
You should consider that this might not be caused by the css itself.
In my case I opened my popup with a link: open popup
So what actually caused the jump to the top was the "#" in the href property of the link.
I removed it and added a noscroll class to my html and body tag:
.noscroll {
overflow: hidden;
}
Keeping the body height 100% from the beginning solved the problem for me.
body{
height:100vh;
overflow:auto;
}
body.with-modal{
overflow:hidden;
}
Use body tag instead of html.
JS Fiddle :- http://jsfiddle.net/SBLgJ/6/
JS Change:-
$(document).ready(function() {
$('#middle a').on('click', function(e) {
e.preventDefault();
$('body').css('overflow-y', 'hidden');
});
});
CSS Change:-
body {
overflow-y:scroll;
}
There is a reported issue for such behavior. (https://github.com/necolas/normalize.css/issues/71)

Categories

Resources