wpfront-scroll-top.js
104 lines
| 1 | /* |
| 2 | WPFront Scroll Top Plugin |
| 3 | Copyright (C) 2013, WPFront.com |
| 4 | Website: wpfront.com |
| 5 | Contact: syam@wpfront.com |
| 6 | |
| 7 | WPFront Scroll Top Plugin is distributed under the GNU General Public License, Version 3, |
| 8 | June 2007. Copyright (C) 2007 Free Software Foundation, Inc., 51 Franklin |
| 9 | St, Fifth Floor, Boston, MA 02110, USA |
| 10 | |
| 11 | */ |
| 12 | |
| 13 | (function() { |
| 14 | var $ = jQuery; |
| 15 | |
| 16 | window.wpfront_scroll_top = function(data) { |
| 17 | var container = $("#wpfront-scroll-top-container").css("opacity", 0); |
| 18 | |
| 19 | var css = {}; |
| 20 | switch (data.location) { |
| 21 | case 1: |
| 22 | css["right"] = data.marginX + "px"; |
| 23 | css["bottom"] = data.marginY + "px"; |
| 24 | break; |
| 25 | case 2: |
| 26 | css["left"] = data.marginX + "px"; |
| 27 | css["bottom"] = data.marginY + "px"; |
| 28 | break; |
| 29 | case 3: |
| 30 | css["right"] = data.marginX + "px"; |
| 31 | css["top"] = data.marginY + "px"; |
| 32 | break; |
| 33 | case 4: |
| 34 | css["left"] = data.marginX + "px"; |
| 35 | css["top"] = data.marginY + "px"; |
| 36 | break; |
| 37 | } |
| 38 | container.css(css); |
| 39 | |
| 40 | if (data.button_width == 0) |
| 41 | data.button_width = "auto"; |
| 42 | else |
| 43 | data.button_width += "px"; |
| 44 | if (data.button_height == 0) |
| 45 | data.button_height = "auto"; |
| 46 | else |
| 47 | data.button_height += "px"; |
| 48 | container.children("img").css({"width": data.button_width, "height": data.button_height}); |
| 49 | |
| 50 | if (data.hide_iframe) { |
| 51 | if ($(window).attr("self") !== $(window).attr("top")) |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | var mouse_over = false; |
| 56 | var hideEventID = 0; |
| 57 | |
| 58 | var fnHide = function() { |
| 59 | clearTimeout(hideEventID); |
| 60 | if (container.is(":visible")) { |
| 61 | container.stop().fadeTo(data.button_fade_duration, 0, function() { |
| 62 | container.hide(); |
| 63 | mouse_over = false; |
| 64 | }); |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | var fnHideEvent = function() { |
| 69 | clearTimeout(hideEventID); |
| 70 | hideEventID = setTimeout(function() { |
| 71 | fnHide(); |
| 72 | }, data.auto_hide_after * 1000); |
| 73 | }; |
| 74 | |
| 75 | $(window).scroll(function() { |
| 76 | if ($(this).scrollTop() > data.scroll_offset) { |
| 77 | container.stop().css("opacity", 1).show(); |
| 78 | if (!mouse_over) { |
| 79 | container.css("opacity", data.button_opacity); |
| 80 | if (data.auto_hide) { |
| 81 | fnHideEvent(); |
| 82 | } |
| 83 | } |
| 84 | } else { |
| 85 | fnHide(); |
| 86 | } |
| 87 | }); |
| 88 | |
| 89 | container |
| 90 | .hover(function() { |
| 91 | clearTimeout(hideEventID); |
| 92 | mouse_over = true; |
| 93 | $(this).css("opacity", 1); |
| 94 | }, function() { |
| 95 | $(this).css("opacity", data.button_opacity); |
| 96 | mouse_over = false; |
| 97 | fnHideEvent(); |
| 98 | }) |
| 99 | .click(function() { |
| 100 | $("html, body").animate({scrollTop: 0}, data.scroll_duration); |
| 101 | return false; |
| 102 | }); |
| 103 | }; |
| 104 | })(); |