Advertisement
ghotriw

gtrw helper

Apr 21st, 2015
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 6.34 KB | None | 0 0
  1. /*!
  2.  * Gtrw Helper; version: 1.1.0 build: 20150721
  3.  * http://ghotriw.pro/
  4.  * Copyright (c) 2015 Andrew Goncharov;
  5.  */
  6. (function(e){
  7.     var methods = {
  8.         info : function() {
  9.             console.info('Gtrw Helper; version: 1.1.0 build: 20150721');
  10.         },
  11.         scrollToTop : function( options ) {
  12.             return this.each(function(){
  13.                 var $this_element = $(this),
  14.                     defaults = {
  15.                         'page_width': 980,
  16.                         'bottom': 20,
  17.                         'top': 'auto',
  18.                         'vertical_alignment': false,
  19.                         'horizontal_position': 'press_the_right',
  20.                         'visible_position': 100,
  21.                         'speed': 300,
  22.                         'duration': 400
  23.                     },
  24.                     settings = e.extend(defaults, options);
  25.  
  26.                 $this_element.css({
  27.                     'display': 'none',
  28.                     'position': 'fixed',
  29.                     'cursor': 'pointer',
  30.                     'right': '50%',
  31.                     'margin-right': -settings.page_width /2 - $this_element.outerWidth(),
  32.                     'bottom': settings.bottom + 'px'
  33.                 });
  34.  
  35.                 if(settings.vertical_alignment) {
  36.                     var this_height = $this_element.outerHeight();
  37.                     $this_element.css({
  38.                         'bottom': 'auto',
  39.                         'top': '50%',
  40.                         'margin-top': -this_height/2
  41.                     })
  42.                 }
  43.  
  44.                 if(settings.horizontal_position != 'press_the_right') {
  45.                     $this_element.css({
  46.                         'margin-left': $this_element.css('margin-left'),
  47.                         'left': settings.horizontal_position + 'px'
  48.                     })
  49.                 }
  50.  
  51.                 if(settings.top != 'auto') {
  52.                     $this_element.css({
  53.                         'margin-left': $this_element.css('margin-left'),
  54.                         'bottom': 'auto',
  55.                         'top': settings.top + 'px'
  56.                     })
  57.                 }
  58.  
  59.                 function show_button(){
  60.                     var page_position = $(window).scrollTop();
  61.                     if(page_position > settings.visible_position) {
  62.                         $($this_element).fadeIn(settings.duration);
  63.                     } else {
  64.                         $($this_element).fadeOut(settings.duration);
  65.                     }
  66.                 }
  67.                 show_button();
  68.  
  69.                 $(window).on('scroll', function() {
  70.                     show_button();
  71.                 });
  72.  
  73.                 $this_element.on('click', function() {
  74.                     $('body, html').animate({
  75.                         scrollTop: 0
  76.                     }, settings.speed)
  77.                 })
  78.             })
  79.         },
  80.         folding : function( options ) {
  81.             return this.each(function() {
  82.                 var $self = $(this),
  83.                     self_class = '.'+$self.attr('class').split(' ').join('.'),
  84.                     defaults = {
  85.                         'button': '.fold_but',
  86.                         'block': '.fold_block',
  87.                         'foldedClass': 'folded',
  88.                         'speed': 200,
  89.                         'folding': true,
  90.                         'afterShow': function() {},
  91.                         'afterHide': function() {}
  92.                     },
  93.                     settings = e.extend(defaults, options);
  94.                 $self.each(function() {
  95.                     var $self = $(this);
  96.                     if(!$self.hasClass(settings.foldedClass)){
  97.                         $self.find(settings.block).hide();
  98.                     } else {
  99.                         $self.find(settings.block).show();
  100.                     }
  101.                 });
  102.  
  103.                 $self.find(settings.button).on('click', function() {
  104.                     if($self.hasClass(settings.foldedClass)) {
  105.                         $self.find(settings.block).stop().slideUp(settings.speed);
  106.                         $self.removeClass(settings.foldedClass);
  107.                         // afterHide callback call:
  108.                         setTimeout(function() {settings.afterHide.call();},1)
  109.                     } else {
  110.                         if(settings.folding){
  111.                             $(self_class).removeClass(settings.foldedClass);
  112.                             $(settings.block).stop().slideUp(settings.speed);
  113.                         }
  114.                         $self.find(settings.block).stop().slideDown(settings.speed);
  115.                         $self.addClass(settings.foldedClass);
  116.                         // afterShow callback call:
  117.                         setTimeout(function() {settings.afterShow.call();},1)
  118.                     }
  119.                 });
  120.             })
  121.         }
  122.     };
  123.  
  124.     $.fn.gtrwHelper = function( method ) {
  125.         if ( methods[method] ) {
  126.             return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
  127.         } else if ( typeof method === 'object' || ! method ) {
  128.             return methods.info.apply( this, arguments );
  129.         } else {
  130.             $.error( 'Метод с именем ' +  method + ' не существует для jQuery.gtrwHelper' );
  131.         }
  132.     };
  133. })(jQuery);
  134.  
  135.  
  136.  
  137.  
  138. $(function(){
  139.  
  140.     var page_width = $('.page').outerWidth();
  141.  
  142.     $('.scroll_to_top').gtrw_helper(
  143.         'scrollToTop' , {
  144.             page_width: page_width
  145.         }
  146.     );
  147. });
  148.  
  149.  
  150.  
  151.  
  152. var scrollCompensate = function () {
  153.         var inner = document.createElement('p');
  154.         inner.style.width = "100%";
  155.         inner.style.height = "200px";
  156.         var outer = document.createElement('div');
  157.         outer.style.position = "absolute";
  158.         outer.style.top = "0px";
  159.         outer.style.left = "0px";
  160.         outer.style.visibility = "hidden";
  161.         outer.style.width = "200px";
  162.         outer.style.height = "150px";
  163.         outer.style.overflow = "hidden";
  164.         outer.appendChild(inner);
  165.         document.body.appendChild(outer);
  166.         var w1 = inner.offsetWidth;
  167.         outer.style.overflow = 'scroll';
  168.         var w2 = inner.offsetWidth;
  169.         if (w1 == w2) w2 = outer.clientWidth;
  170.         document.body.removeChild(outer);
  171.         return (w1 - w2);
  172.     };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement