Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving element on Window scroll

You'll see in this JsFiddle https://jsfiddle.net/xpvt214o/402281/ there is an image scrolling within another one on window scroll. How can I modify the below so that it doesn't start scrolling until it hits that element (after all the br tags)? I've tried offsetting it but not getting anywhere.

It would also be fine if it just scrolled a lot slower.

jQuery(document).ready(
function() {

var $w = $(window);
var $d = $('.oh');

$(window).scroll(function(event) {
  var st = $w.scrollTop();

  _x = st;

  lastScrollTop = st;

  $d.css('bottom', _x);      

});

});

Here's an example of what I'm trying to achieve https://www.sketchapp.com/

like image 610
sarah3585 Avatar asked Jul 12 '18 10:07

sarah3585


People also ask

How do I make a div move up and down when I'm scrolling the page?

Just add position: fixed; in your div style. I have checked and Its working fine in my code.

How do I scroll to a specific element?

The scrollTo method: The scrollTo() is used to scroll to the specified element in the browser.

How do I fix a scrolling element?

just use position:fixed; for what you want to be fixed when you scroll down.


1 Answers

I found this which does what I'm looking for

https://codepen.io/JTParrett/pen/BkDie

$.fn.moveIt = function(){
var $window = $(window);
var instances = [];

$(this).each(function(){
instances.push(new moveItItem($(this)));
});

window.addEventListener('scroll', function(){
var scrollTop = $window.scrollTop();
instances.forEach(function(inst){
  inst.update(scrollTop);
});
}, {passive: true});
}

var moveItItem = function(el){
this.el = $(el);
this.speed = parseInt(this.el.attr('data-scroll-speed'));
};

moveItItem.prototype.update = function(scrollTop){
  this.el.css('transform', 'translateY(' + -(scrollTop / this.speed) + 
'px)');
};

// Initialization
$(function(){
  $('[data-scroll-speed]').moveIt();
});
like image 180
sarah3585 Avatar answered Oct 10 '22 22:10

sarah3585