dubroe / sticky-div Goto Github PK
View Code? Open in Web Editor NEWA jquery plugin that forces a div to stick to the top of the window if the user tries to scroll past it
A jquery plugin that forces a div to stick to the top of the window if the user tries to scroll past it
Hey
Good job, if a first plugin! I found it here: http://stackoverflow.com/questions/2907367/have-a-div-cling-to-top-of-screen-if-scrolled-down-past-it
I am showing a relatively positioned div, on a jquery ajax complete for adding to a shopping basket, to show a message near the "mini basket". As such, I have made a couple of changes to the code. Commented below. Now it works a treat, even on a mobile (as site is responsive)
(function ($) {
$.fn.sticky_div = function (options) {
var defaults = {
top: 0,
min_screen_width: 0,
bottom: 0,
outer_div: null
};
var options = $.extend(defaults, options);
// added !important to override any current positioning or top.
$("<style type='text/css'> .sticky-div{ position:fixed !important; top:" + options.top + "px !important;} </style>").appendTo("head");
var selector = this;
// added a class to the anchor, and checked for it, so that the anchor <div/> is only added the once.
selector.each(function () {
if ($(this).siblings(".sticky-anchor").length === 0) {
$(this).before("<div class='sticky-anchor'></div>");
}
});
// call to refactored method, so that it is not only a window scroll which triggers it (if, for example, my add to basket button is at the bottom of the page, and my anchor is at the top
$.sticky_div(selector, options);
$(window).scroll(function () {
if ($(window).width() >= options.min_screen_width) {
// called again on scroll.
$.sticky_div(selector, options);
}
});
};
// refactored method, which does the magic
$.sticky_div = function (selector, options) {
var window_top = $(window).scrollTop();
var window_height = $(window).height();
selector.each(function () {
var div_top = $(this).prev().offset().top;
var div_height = $(this).outerHeight();
if ((!options.outer_div || (div_height < options.outer_div.height())) && (window_top > (div_top - options.top)) && (div_height < (window_height - options.bottom))) {
$(this).addClass('sticky-div');
} else {
$(this).removeClass('sticky-div');
}
});
}
})(jQuery);
When I put a value on bottom like this "bottom: 515," the code doesn't work anymore. I just want to stop the div when it hits the footer div. Am I doin it right?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.