My question is similar to: How to increase/decrease current margin at a number by jquery? however I cannot adapt the solution to match my code. I keep getting errors.
My CSS defines $logo variable with a margin-top of -82px. In my JS below, the margin-top value is being adjusted on scroll but it assumes the start value for margin-top is 0. How do I say its -82px?
Here's a jsfiddle of the glitch: http://jsfiddle.net/5DAa9/ Carefully scroll down and you will notice the box jump.
var $logo = $('.logo.abbr');
var windowScroll;
var $curValue = $( '.logo.abbr' ).css( "margin-top" );
// Functional parallaxing calculations
function slidingTitle() {
//Get scroll position of window
windowScroll = $(this).scrollTop();
//Slow scroll of .logo and fade it out
$logo.css({
'margin-top' : ($curValue-(windowScroll/3)+"px") // Assuming this is the line I need to change??
});
}
Heres a quick fix...
windowScroll = $(this).scrollTop()+250;
Why this is happening I have no idea. Im gonna keep poking around to see if I can figure it out but this is working for me here
http://jsfiddle.net/5DAa9/1/
Edit:
I have found the problem!!
windowScroll = $(this).scrollTop(); This line will return 0 if the page is at the top. You have set your margin to be -82px before the scroll so that just gets erased on the first scroll and set back to 0 or whatever the scroll is divided by 3. So you want to do that calculation and then subtract the 82 pixels again to keep it in the "same place" or prevent the jump.
$logo.css({
'margin-top' : -(windowScroll/3)-82+"px"
});
Its kind of an ugly fix but thats whats going on. I assume that 250 of whatever scaling scrollTop returns is equivalent to 82px. Heres an updated Fiddle: http://jsfiddle.net/5DAa9/3/
Related
Using jQuery, how do you scroll a div by N more pixels?
So far I tried
$("div.foo").scrollTop(75); //scroll down by 75 pixels
From the jQuery docs for scrollTop:
Description: Get the current vertical position of the scroll bar for
the first element in the set of matched elements or set the vertical
position of the scroll bar for every matched element.
The version of the function you are calling sets the vertical position of the scroll bar from the top of the element, not from the current position. So in order to scroll down from the current position you first have to retrieve your current position. There are a few ways to do that, but you could do something like:
var $foo = $("div.foo");
$foo.scrollTop($foo.scrollTop() + 75); // scroll 75px down from current
If you are looking for Vanilla JavaScript solution like I was, here it is.
const selectedElement = document.querySelector('div.foo');
selectedElement.scrollTop = 75;
querySelector documentation,
scrollTop documentation
the following code may help and you may see this link .
and you can pixel value from window atrributes.Thank you.
var d = $('#div1');
d.scrollTop(d.prop("scrollHeight"));
I love that this code works, but I cannot, for anything, wrap my head around WHY it's working?
Here is the jfidddle
Here is the code:
jQuery(document).ready(function($) {
clone = $('div').clone();
$('div').after(clone);
$('div:last').hide();
offset = $('div:first').offset();
var fromtop = offset.top;
$(document).scroll(function() {
doc = $(this);
dist = $(this).scrollTop();
if (dist >= fromtop) {
$('div:last').show();
$('div:first').css({
'position': 'fixed'
});
} else {
$('div:first').css({
'position': 'static'
});
$('div:last').hide();
}
});
});
I guess I am not understanding how scrolltop and offset are interacting or what they REALLY are, as in their true positions on the page. The code says if ScrollTop (the scrollbar position?) is higher than the value of the div's offsettop , then make the div sticky. But if ScrollTop is the position of the scrollbar, isn't it true that sometimes the scroll bar position could be lower than the div's position BEFORE the div is at the top of the page? What is it about being at the top of the page (offsettop of 0?)--and only at the top of the page, never before-- that makes offsettop a smaller value than scrolltop?
Really confused, and I don't want to just copy the code without understanding what it's really doing.
scroll Top is actually how many pixels 'up' the page has moved (or how many pixels you have moved down the page)
Basically all that happens is the .offset sees how far down the page (from the top of the page) the 'sticky' menu is
When you scroll to that point the bar becomes fixed (which is basically relative to the window instead of the document)
When you scroll back up it just switches back to being positioned in the document.
For clarity
.offset = 200px say - this is how far down the document the sticky menu is
.scrollTop - is 0 when the page loads
When you scroll down the page 201px
.scrollTop > .offSet -> so make the bar fixed (remember fixed is relative to the window - not the document)
If you scroll back up the process is reversed.
It's actually very simple. Let me try if I can make it a bit clear to you:
Whenever you want something (let's say some div) to get fixed on top as you scroll down, you need two things:
You need the current vertical position of your div. And you calculate that by using offset().top
You need to track how much user has scrolled. And you calculate that by using scrollTop()
So in your case, if the current position of your div is top: 100, then as soon as your scrollbar reaches the number 101, your div will get the class of .fixed
By default, the scrollbar vertical position is 0 when the page loads.
Im trying to make an element move a little slower than the rest of the window scroll (parallax if you like)...
I'm using the following...
$(window).scroll(function() {
'use strict';
console.log($('section').offset().top);
$('img').css({
'margin-top' : -($('section').offset().top/3)+"px",
});
});
Only the section offset remains static throughout the scroll at 130px whereas it should constantly be changing?
Any ideas what i'm doing wrong?
I think the function you are looking for is $(window).scrollTop() which will return the amount the page is scrolled down at any given point.
var scroll = $(window).scrollTop();
Let me start of by saying, I'm just now learning JS and Jquery, so my knowledge is very limited.
I've been looking around for 2 days now, and tried all sorts of combinations. But I just can't get this to work.
Below is an example of the layout
I'm looking for a way to trigger an event when div 1 is X px from the top of the screen. Or when div 1 collides with div 2.
What I'm trying to accomplish is to change the css of div 2 (the fixed menu) when div 1 is (in this case) 100px from the top of screen (browser window). Alternatively, when div1 passes div2 (I'm using responsive design, so the fixed height from top might become a problem on smaller screens right? Seeing as the header for example won't be there on a hand held.). So maybe collision detection is better here? Would really appreciate some thoughts and input on this matter.
Another issue is, div2 has to revert back to is previous css once div1 passes it (going back (beyond the 100px)).
This is what I have but it has no effect
$(document).ready(function() {
var content = $('#div1');
var top = $('#div2');
$(window).on('scroll', function() {
if(content.offset().top <= 100) {
top.css({'opacity': 0.8});
}else{
top.css({'opacity': 1});
}
});
});
I am not sure of the reason but $("#content").offset().top was giving a constant value on console. So I added window.scrollTOp() to check its distance from top, here is how it works,
$(document).ready(function() {
var top = $("#menu");
$(window).on('scroll', function(){
if(($('#content').offset().top - $(window).scrollTop()) <= 100){
top.css({'opacity': 0.4});
}else{
top.css({'opacity': 1});
}
});
});
And DEMO JSFIDDLE....
I have the following code below that changes a div's position to fixed once an element scrollTop is greater than a number. I am trying to either amend this script or find a different solution so that the div will scroll between a range and STOP scrolling at some point ( so the div doesn't go off the page or overlap with footer elements.
I don't know if the right way is to say that IF scrollTop is greater than 150 then position=fixed, and if it's greater than 600 then position goes back to absolute, or if there's a better way, like distance from the bottom...
AND I use MooTools, not jQuery. I know there are a few jQuery options / plugins that do this. Thanks in advance!
window.onscroll = function()
{
if( window.XMLHttpRequest ) { // IE 6 doesnt implement position fixed nicely...
if (document.documentElement.scrollTop > 150) {
$('tabber').style.position = 'fixed';
$('tabber').style.top = '0';
} else {
$('tabber').style.position = 'absolute';
$('tabber').style.top = 'auto';
}
}
}
the script above is wrong on many levels.
don't use window.onscroll but window.addEvent("scroll", function() {});
cache selectors. using $("tabber") 3 times on each scroll when the element does not change is expensive.
just do var tabber = $("tabber") and reference that.
you don't need to do
$("tabber").style.position = ...
$("tabber").style.top = ...
do:
tabber.setStyles({
position: "fixed",
top: 12123,
right: 24
});
There are mootools plugins for this, eg scrollSpy by David Walsh: http://davidwalsh.name/mootools-scrollspy
it can allow you to set scripted events upon reaching various scrolling destinations or events, look at the examples.
or you could just write it yourself, eg, this took me 15 mins to do:
http://jsfiddle.net/dimitar/u9J2X/ (watch http://jsfiddle.net/dimitar/u9J2X/show/) - it stops being fixed when it reaches to 20 px of the footer. and then goes back to fixed if scrolling up again.