wpfront-scroll-top.js
127 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 | function bind_actions(data) { |
| 17 | var container = $("#wpfront-scroll-top-container"); |
| 18 | |
| 19 | var mouse_over = false; |
| 20 | var hideEventID = 0; |
| 21 | |
| 22 | var fnHide = function () { |
| 23 | clearTimeout(hideEventID); |
| 24 | if (container.is(":visible")) { |
| 25 | container.stop().fadeTo(data.button_fade_duration, 0, function () { |
| 26 | container.removeClass("show"); |
| 27 | mouse_over = false; |
| 28 | }); |
| 29 | } |
| 30 | }; |
| 31 | |
| 32 | var fnHideEvent = function () { |
| 33 | if(!data.auto_hide) |
| 34 | return; |
| 35 | |
| 36 | clearTimeout(hideEventID); |
| 37 | hideEventID = setTimeout(function () { |
| 38 | fnHide(); |
| 39 | }, data.auto_hide_after * 1000); |
| 40 | }; |
| 41 | |
| 42 | var scrollHandled = false; |
| 43 | var fnScroll = function () { |
| 44 | if (scrollHandled) |
| 45 | return; |
| 46 | |
| 47 | scrollHandled = true; |
| 48 | |
| 49 | if ($(window).scrollTop() > data.scroll_offset) { |
| 50 | container.stop().css("opacity", mouse_over ? 1 : data.button_opacity).addClass("show"); |
| 51 | if (!mouse_over) { |
| 52 | fnHideEvent(); |
| 53 | } |
| 54 | } else { |
| 55 | fnHide(); |
| 56 | } |
| 57 | |
| 58 | scrollHandled = false; |
| 59 | }; |
| 60 | |
| 61 | $(window).on('scroll', fnScroll); |
| 62 | fnScroll(); |
| 63 | |
| 64 | container |
| 65 | .on('mouseenter', function() { |
| 66 | clearTimeout(hideEventID); |
| 67 | mouse_over = true; |
| 68 | $(this).css("opacity", 1); |
| 69 | }).on('mouseleave', function() { |
| 70 | $(this).css("opacity", data.button_opacity); |
| 71 | mouse_over = false; |
| 72 | fnHideEvent(); |
| 73 | }).on('click', function(e) { |
| 74 | if(data.button_action === "url") { |
| 75 | return true; |
| 76 | } else if(data.button_action === "element") { |
| 77 | e.preventDefault(); |
| 78 | |
| 79 | var element = $(data.button_action_element_selector).first(); |
| 80 | var container = $(data.button_action_container_selector); |
| 81 | |
| 82 | var offset = element.offset(); |
| 83 | if(offset == null) |
| 84 | return false; |
| 85 | |
| 86 | var contOffset = container.last().offset(); |
| 87 | if(contOffset == null) |
| 88 | return false; |
| 89 | |
| 90 | data.button_action_element_offset = parseInt(data.button_action_element_offset); |
| 91 | if(isNaN(data.button_action_element_offset)) |
| 92 | data.button_action_element_offset = 0; |
| 93 | |
| 94 | var top = offset.top - contOffset.top - data.button_action_element_offset; |
| 95 | |
| 96 | container.animate({scrollTop: top}, data.scroll_duration); |
| 97 | |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | e.preventDefault(); |
| 102 | $("html, body").animate({scrollTop: 0}, data.scroll_duration); |
| 103 | return false; |
| 104 | }); |
| 105 | } |
| 106 | |
| 107 | $(window).on('load', function() { |
| 108 | function process(d) { |
| 109 | if (d.data.hide_iframe) { |
| 110 | if ($(window).attr("self") !== $(window).attr("top")) { |
| 111 | return; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if(d.css !== null) { |
| 116 | $('<style>').text(d.css).appendTo('head'); |
| 117 | } |
| 118 | |
| 119 | $('body').append(d.html); |
| 120 | |
| 121 | bind_actions(d.data); |
| 122 | } |
| 123 | |
| 124 | process(wpfront_scroll_top_data.data); |
| 125 | }); |
| 126 | })(); |
| 127 |