Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from JeffenCheung/master
Create bottom-sticker.js
- Loading branch information
Showing
4 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/*! | ||
* bootm sticker | ||
* Copyright 2016-2020 Pactear. | ||
* Licensed under the MIT license | ||
* @author JeffenCheung@gmail.com | ||
* @date 2016-4-8 | ||
* @version 0.0.1 | ||
*/ | ||
(function($) { | ||
|
||
$.fn.bottomSticker = function (options) { | ||
var config = { | ||
bottom: '0px', | ||
width: '100%' | ||
}; | ||
if (options){ $.extend(config, options); } | ||
|
||
return this.each( function() { | ||
var o = $(this) | ||
, cId = o.attr("id") + "-copy"; | ||
var c = "." + cId; | ||
|
||
var $win = $(window) | ||
, isFixed = 0; | ||
|
||
function processScroll() { | ||
var scrollTop = $win.scrollTop() | ||
, cTop = $(c).offset().top | ||
, oTop = o.offset().top; | ||
if (oTop > cTop && !isFixed) { | ||
isFixed = 1; | ||
} else if (oTop <= cTop && isFixed) { | ||
isFixed = 0; | ||
} | ||
|
||
isFixed ? $(c).show().width(getWidth()) | ||
: $(c).hide(); | ||
|
||
} | ||
function getWidth() { | ||
var width = o.width(); | ||
if (width == 0) | ||
width = config['width']; | ||
return width; | ||
} | ||
|
||
$win.on('scroll', processScroll); | ||
$win.on('resize', processScroll); | ||
|
||
var clone = o.clone(true) | ||
.attr("id", cId) | ||
.addClass(cId) | ||
.css({'position': 'fixed' | ||
, 'bottom': config['bottom'] | ||
, 'width': getWidth()}); | ||
o.after(clone); | ||
|
||
processScroll(); | ||
|
||
}); // return each | ||
}; | ||
|
||
})(jQuery); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/*! | ||
* bootm sticker | ||
* Copyright 2016-2020 Pactear. | ||
* Licensed under the MIT license | ||
* @author JeffenCheung@gmail.com | ||
* @date 2016-4-8 | ||
* @version 0.0.1 | ||
*/ | ||
(function(a){a.fn.bottomSticker=function(c){var b={bottom:"0px",width:"100%"};if(c){a.extend(b,c)}return this.each(function(){var h=a(this),e=h.attr("id")+"-copy";var k="."+e;var j=a(window),g=0;function f(){var n=j.scrollTop(),m=a(k).offset().top,l=h.offset().top;if(l>m&&!g){g=1}else{if(l<=m&&g){g=0}}g?a(k).show().width(d()):a(k).hide()}function d(){var l=h.width();if(l==0){l=b.width}return l}j.on("scroll",f);j.on("resize",f);var i=h.clone(true).attr("id",e).addClass(e).css({position:"fixed",bottom:b.bottom,width:d()});h.after(i);f()})}})(jQuery); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters