Scrollbar issue with JS tabs - javascript

I'm brand new to javascript, so the problem might actually lie in how much I know or how to word searches for this, but I haven't found the same problem anywhere else:
When making a set of tabs with script, they works fine, but the scrollbar will stay the same on each tab. For example, if I go to the second tab and scroll all the way down and then click on the third tab, instead of going to the top, it will stay at the bottom because that's where it was on the second tab.
This is what I'm working with: http://jsfiddle.net/z7uxfbws/
jQuery(document).ready(function() {
jQuery('.tabs .tab-links a').on('click', function(e) {
var currentAttrValue = jQuery(this).attr('href');
// Show/Hide Tabs
jQuery('.tabs ' + currentAttrValue).fadeIn(400).siblings().hide();
// Change/remove current tab to active
jQuery(this).parent('li').addClass('active').siblings().removeClass('active');
e.preventDefault(e);
});
});
If you could explain what the problem is, how to fix it, and why it works, that would be absolutely wonderful.
Thank you for taking the time to read!

The scrollbar is on the parent element, .tab-content.
The elements that are being manipulated (hidden/shown) are the children elements, .tab.
Therefore the scrollbar on the parent element is always there. Thus, when switching tabs, the scrollbar's position doesn't change.
In order to work around this, you could move the scrollbar to the children elements rather than the parent.
Updated Example
.tab {
display: none;
overflow: auto; /* Moved this from the parent element */
height: 100%;
padding: 10px; /* This *was* on the parent element, too. */
box-sizing: border-box; /* Include the padding in the
element's width/height calculations */
}
... alternatively, you could also just scroll .tab-content's scrollbar to the top each time a tab is changed.
Updated Example
$('.tab-content').scrollTop(0);

Related

Navbar Not Switching To Fixed When I Scroll By It

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)

Lock page scroll temporarily in the middle of the long page

Ok, so there is a webpage with a long list. In the middle of the list I'd like to lock the scrolling and later enable it again. How would this be possible, so that it would behave nicely in modern mobile browsers?
One solution I tried is to set body position style to fixed and the setting top style to the scrollTop value prior to the setting position to fixed. There is this thing about position: fixed - as soon as it is set, the page will be jumped to the top. Problem is that on iOS Safari the page is sort of flashes when you enable/disable scroll, and it also gets really laggy behaviour on Android Chrome.
Any better hints?
Update: I have a sidebar menu with list of items, and while the main page should be locked, sidebar menu should remain scrollable.
Just add a class to the html-tag every time you want to look the screen. I use this method on js modals or lightboxes.
You can do this simply by adding the overflow attribute.
.
CSS:
html.-is-locked {
overflow: hidden !important;
}
.
JS:
Now you just have to add/remove the class with javascript:
//Get HTML element
var html = document.querySelector('html');
//Activate
html.classList.add('-is-locked');
//Deactivate
html.classList.remove('-is-locked');
//Toggle
html.classList.toggle('-is-locked');
Try this on the body instead of position:fixed:
body.locked{
height: 100vh;
overflow: hidden;
}
It will keep the scroll position but prevent scrolling.

Hiding an element with hide() causes a page "jump"

I'm trying to create an effect where I display a big logo on page load. When the user scrolls pass the logo and navigation, I want to display a fixed nav bar with a smaller logo. I then want to hide the big logo so that when the user scrolls to the top they still see the fixed nav bar (i.e. the big logo and original navigation stay hidden).
However, when I remove a big block element with the .hide() property it causes the page to "jump" as the display:none property gets set. This reduces the usability of the page, as the location jumps the size of the element that was removed, potentially confusing users.
Is there a way I can get the effect I want, while still providing a smooth experience to the user? I've been thinking of potential options, but have been drawing blanks. Hoping you guys can inspire me :)
A simple JS fiddle can be seen here: http://jsfiddle.net/darudude/vA5WG/ (Note: You'll have to increase the result section to 720+px to get it to work properly - I'm still working on the responsive part)
The code in question:
function UpdateTableHeaders() {
var menu = $(".main_nav_menu"),
offset_top = menu.offset().top;
var scrollTop = $(window).scrollTop();
if (scrollTop > (offset_top + menu.height()))
{
$(".clone").addClass("floating_header");
$(".big_logo").hide();
}
}
$(window).scroll(function(){
UpdateTableHeaders();
});
You can try this ,
Add a new style
<style>
.hide {
position: absolute !important;
top: -9999px !important;
left: -9999px !important;
}
</style>
And change your JS to
$(".big_logo").addClass('hide');
Instead of
$(".big_logo").hide();
Use visibility:hidden then
$(".big_logo").css('visibility','hidden');
Maybe it is because a different browser - margin/padding thing. Have you tried to add this to the body element (or to the container element if it inherits some margins/paddings)
body{
margin:0;
padding:0;
width:100%;
}

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