(function ($, window, document) { 'use strict'; // main function $.fn.scrollup = function (options) { // ensure that only one scrollup exists if (!$.data(document.body, 'scrollup')) { $.data(document.body, 'scrollup', true); $.fn.scrollup.init(options); } }; // init $.fn.scrollup.init = function (options) { // define vars var o = $.fn.scrollup.settings = $.extend({}, $.fn.scrollup.defaults, options), triggervisible = false, animin, animout, animspeed, scrolldis, scrollevent, scrolltarget, $self; // create element if (o.scrolltrigger) { $self = $(o.scrolltrigger); } else { $self = $('', { id: o.scrollname, href: '#top' }); } // set scrolltitle if there is one if (o.scrolltitle) { $self.attr('title', o.scrolltitle); } $self.appendto('body'); // if not using an image display text if (!(o.scrollimg || o.scrolltrigger)) { $self.html(o.scrolltext); } // minimum css to make the magic happen $self.css({ display: 'none', position: 'fixed', zindex: o.zindex }); // active point overlay if (o.activeoverlay) { $('
', { id: o.scrollname + '-active' }).css({ position: 'absolute', 'top': o.scrolldistance + 'px', width: '100%', bordertop: '1px dotted' + o.activeoverlay, zindex: o.zindex }).appendto('body'); } // switch animation type switch (o.animation) { case 'fade': animin = 'fadein'; animout = 'fadeout'; animspeed = o.animationspeed; break; case 'slide': animin = 'slidedown'; animout = 'slideup'; animspeed = o.animationspeed; break; default: animin = 'show'; animout = 'hide'; animspeed = 0; } // if from top or bottom if (o.scrollfrom === 'top') { scrolldis = o.scrolldistance; } else { scrolldis = $(document).height() - $(window).height() - o.scrolldistance; } // scroll function scrollevent = $(window).scroll(function () { if ($(window).scrolltop() > scrolldis) { if (!triggervisible) { $self[animin](animspeed); triggervisible = true; } } else { if (triggervisible) { $self[animout](animspeed); triggervisible = false; } } }); if (o.scrolltarget) { if (typeof o.scrolltarget === 'number') { scrolltarget = o.scrolltarget; } else if (typeof o.scrolltarget === 'string') { scrolltarget = math.floor($(o.scrolltarget).offset().top); } } else { scrolltarget = 0; } // to the top $self.click(function (e) { e.preventdefault(); $('html, body').animate({ scrolltop: scrolltarget }, o.scrollspeed, o.easingtype); }); }; // defaults $.fn.scrollup.defaults = { scrollname: 'scrollup', // element id scrolldistance: 300, // distance from top/bottom before showing element (px) scrollfrom: 'top', // 'top' or 'bottom' scrollspeed: 300, // speed back to top (ms) easingtype: 'linear', // scroll to top easing (see http://easings.net/) animation: 'fade', // fade, slide, none animationspeed: 200, // animation in speed (ms) scrolltrigger: false, // set a custom triggering element. can be an html string or jquery object scrolltarget: false, // set a custom target element for scrolling to. can be element or number scrolltext: 'scroll to top', // text for element, can contain html scrolltitle: false, // set a custom title if required. defaults to scrolltext scrollimg: false, // set true to use image activeoverlay: false, // set css color to display scrollup active point, e.g '#00ffff' zindex: 2147483647 // z-index for the overlay }; // destroy scrollup plugin and clean all modifications to the dom $.fn.scrollup.destroy = function (scrollevent) { $.removedata(document.body, 'scrollup'); $('#' + $.fn.scrollup.settings.scrollname).remove(); $('#' + $.fn.scrollup.settings.scrollname + '-active').remove(); // if 1.7 or above use the new .off() if ($.fn.jquery.split('.')[1] >= 7) { $(window).off('scroll', scrollevent); // else use the old .unbind() } else { $(window).unbind('scroll', scrollevent); } }; $.scrollup = $.fn.scrollup; })(jquery, window, document);