com web/php: Refactor 'to top' functions (E_TOO_MUCH_JQ UERY): include/footer.inc js/common.js
| From: | Dejan Marjanovic | Date: | Sun, 15 Dec 2013 17:48:29 +0000 |
| Subject: | com web/php: Refactor 'to top' functions (E_TOO_MUCH_JQ UERY): include/footer.inc js/common.js | ||
| Groups: | php.webmaster | ||
| Request: | Send a blank email to php-webmaster+get-17635@lists.php.net to get a copy of this message | ||
Commit: 204d7a45f5cab3770a5a703c569ad4a8ade27b9f
Author: Dejan Marjanovic <dejan.marjanovic@gmail.com> Sun, 15 Dec 2013 18:48:29
+0100
Parents: b1f27ebc8fa8e569a77e2434e825587b0db34853
Branches: master
Link: http://git.php.net/?p=web/php.git;a=commitdiff;h=204d7a45f5cab3770a5a703c569ad4a8ade27b9f
Log:
Refactor 'to top' functions (E_TOO_MUCH_JQUERY)
Changed paths:
M include/footer.inc
M js/common.js
Diff:
diff --git a/include/footer.inc b/include/footer.inc
index 9de510b..078a695 100644
--- a/include/footer.inc
+++ b/include/footer.inc
@@ -71,6 +71,8 @@ UserVoice.push(['showTab', 'classic_widget', {
}]);
</script>
+<a id="toTop" href="javascript:;"><span
id="toTopHover"></span><img width="40" height="40"
alt="To Top" src="/images/to-top@2x.png"></a>
+
</body>
</html>
diff --git a/js/common.js b/js/common.js
index bd8b296..8ad3548 100755
--- a/js/common.js
+++ b/js/common.js
@@ -165,50 +165,40 @@ $(document).ready(function() {
/*{{{ Scroll to top */
(function() {
- var settings = {
- text: 'To Top',
- min: 200,
- inDelay:600,
- outDelay:400,
- containerID: 'toTop',
- containerHoverID: 'toTopHover',
- scrollSpeed: 400,
- easingType: 'linear'
- };
- var containerIDhash = '#' + settings.containerID;
- var containerHoverIDHash = '#'+settings.containerHoverID;
-
- $('body').append('<a href="#"
id="'+settings.containerID+'" onclick="return false;"><img
src="/images/to-top@2x.png" width="40" height="40"
alt="'+settings.text+'"/></a>');
- $(containerIDhash).hide().click(function(){
- $('html, body').animate({scrollTop:0}, settings.scrollSpeed,
settings.easingType);
- $('#'+settings..containerHoverID, this).stop().animate({'opacity':
0 }, settings.inDelay, settings.easingType);
- return false;
- })
- .prepend('<span
id="'+settings.containerHoverID+'"></span>')
- ..hover(function() {
- $(containerHoverIDHash, this).stop().animate({
- 'opacity': 1
- }, 600, 'linear');
- }, function() {
- $(containerHoverIDHash, this).stop().animate({
- 'opacity': 0
- }, 700, 'linear');
- });
- $(window).scroll(function() {
- var sd = $(window).scrollTop();
- if (typeof document.body.style.maxHeight === "undefined") {
- $(containerIDhash).css({
- 'position': 'absolute',
- 'top': $(window).scrollTop() + $(window).height() - 50
- });
- }
- if ( sd > settings.min ) {
- $(containerIDhash).fadeIn(settings.inDelay);
- } else {
- $(containerIDhash).fadeOut(settings.outDelay);
- }
- });
+ var settings = {
+ text: 'To Top',
+ min: 200,
+ inDelay: 600,
+ outDelay: 400,
+ containerID: 'toTop',
+ containerHoverID: 'toTopHover',
+ scrollSpeed: 400,
+ easingType: 'linear'
+ };
+
+ var toTopHidden = true;
+ var toTop = $('#' + settings.containerID);
+
+ toTop.click(function(e) {
+ e.preventDefault();
+ $.scrollTo(0, settings.scrollSpeed, {easing: settings.easingType});
+ });
+
+ var toTopHidden = true;
+ $(window).scroll(function() {
+ var sd = $(this).scrollTop();
+ if (sd > settings.min && toTopHidden)
+ {
+ toTop.fadeIn(settings.inDelay);
+ toTopHidden = false;
+ }
+ else if(sd <= settings.min && ! toTopHidden)
+ {
+ toTop.fadeOut(settings.outDelay);
+ toTopHidden = true;
+ }
+ });
})();
/*}}}*/