diff --git a/README.md b/README.md index ac92f4d..a0e1388 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,19 @@ Default initialization ```javascript ``` diff --git a/jquery.slideandswipe.js b/jquery.slideandswipe.js index 1e0f934..1f9eac1 100644 --- a/jquery.slideandswipe.js +++ b/jquery.slideandswipe.js @@ -3,7 +3,7 @@ * * @copyright Copyright 2013-2015 Joan claret * @license MIT - * @author Joan Claret Teruel + * @author Joan Claret Teruel | Lenivene Bezerra * * Licensed under The MIT License (MIT). * Copyright (c) Joan Claret Teruel @@ -30,141 +30,198 @@ ;(function($, document, window, undefined) { - 'use strict'; - - var slideAndSwipe = - - $.fn.slideAndSwipe = function(options) { - - var nav = $(this); // get the element to swipe - var navWidth = -nav.outerWidth(); - var transInitial = navWidth; - - // get settings - var settings = $.extend({ - triggerOnTouchEnd : true, - swipeStatus : swipeStatus, - allowPageScroll : 'vertical', - threshold : 100, - excludedElements : 'label, button, input, select, textarea, .noSwipe', - speed : 250 - - }, options ); - - nav.swipe(settings); - - /** - * Catch each phase of the swipe. - * move : we drag the navigation - * cancel : open navigation - * end : close navigation - */ - function swipeStatus(event, phase, direction, distance) { - if(phase == 'start') { - if(nav.hasClass('ssm-nav-visible')) { - transInitial = 0; - } else { - transInitial = navWidth; - } - } - var mDistance; - - if (phase == 'move' && (direction == 'left')) { - if(transInitial < 0) { - - mDistance = transInitial - distance; - } else { - mDistance = -distance; - } - - scrollNav(mDistance, 0); - - } else if (phase == 'move' && direction == 'right') { - if(transInitial < 0) { - mDistance = transInitial + distance; - } else { - mDistance = distance; - } - scrollNav(mDistance, 0); - } else if (phase == 'cancel' && (direction == 'left') && transInitial === 0) { - scrollNav(0, settings.speed); - } else if (phase == 'end' && (direction == 'left')) { - - hideNavigation(); - } else if ((phase == 'end' || phase == 'cancel') && (direction == 'right')) { - console.log('end'); - } - } - - /** - * Browser detect - */ - function isSafari() { - return /Safari/.test(navigator.userAgent) && /Apple Computer/.test(navigator.vendor); - } - - function isChrome() { - return /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor); - } - - /** - * Manually update the position of the nav on drag - */ - function scrollNav(distance, duration) { - nav.css('transition-duration', (duration / 1000).toFixed(1) + 's'); - - if(distance >= 0) { - distance = 0; - } - if(distance <= navWidth) { - distance = navWidth; - } - if(isSafari() || isChrome()) { - nav.css('-webkit-transform', 'translate(' + distance + 'px,0)'); - } - else{ - nav.css('transform', 'translate(' + distance + 'px,0)'); - } - if(distance == '0') { - $('.ssm-toggle-nav').addClass('ssm-nav-visible'); - $('html').addClass('is-navOpen'); - $('.ssm-overlay').fadeIn(); - } - } - - /** - * Open / close by click on burger icon - */ - var hideNavigation = (function() { - nav.removeClass('ssm-nav-visible'); - scrollNav(navWidth, settings.speed); - $('html').removeClass('is-navOpen'); - $('.ssm-overlay').fadeOut(); - }); - - var showNavigation = (function() { - nav.addClass('ssm-nav-visible'); - scrollNav(0, settings.speed); - }); - - $('.ssm-toggle-nav').click(function(e) { - if(nav.hasClass('ssm-nav-visible')) { - hideNavigation(); - } - else{ - showNavigation(); - } - e.preventDefault(); - }); - } - ; + 'use strict'; + + var slideAndSwipe = + + $.fn.slideAndSwipe = function(options) { + + var nav = $(this); // get the element to swipe + var navWidth = -nav.outerWidth(); + var transInitial = navWidth; + + if( "object" === typeof options && "boolean" === typeof options.hide ){ + if( options.hide && "undefined" !== typeof hideNavigation ){ + hideNavigation(); + } + } + + if( "object" === typeof options && "boolean" === typeof options.show ){ + if( options.show || options.show && ( "boolean" === typeof options.hide && ! options.hide ) ){ + showNavigation(); + } + } + + // get settings + var settings = $.extend({ + triggerOnTouchEnd : true, + swipeStatus : swipeStatus, + allowPageScroll : 'vertical', + threshold : 100, + excludedElements : 'label, button, input, select, textarea, .noSwipe', + speed : 250 + + }, options ); + + nav.swipe(settings); + + /** + * Catch each phase of the swipe. + * move : we drag the navigation + * cancel : open navigation + * end : close navigation + */ + function swipeStatus(event, phase, direction, distance) { + if(phase == 'start') { + if(nav.hasClass('ssm-nav-visible')) { + transInitial = 0; + } else { + transInitial = navWidth; + } + } + var mDistance; + + if (phase == 'move' && (direction == 'left')) { + if(transInitial < 0) { + + mDistance = transInitial - distance; + } else { + mDistance = -distance; + } + + scrollNav(mDistance, 0); + + } else if (phase == 'move' && direction == 'right') { + if(transInitial < 0) { + mDistance = transInitial + distance; + } else { + mDistance = distance; + } + scrollNav(mDistance, 0); + } else if (phase == 'cancel' && (direction == 'left') && transInitial === 0) { + scrollNav(0, settings.speed); + } else if (phase == 'end' && (direction == 'left')) { + + hideNavigation(); + } else if ((phase == 'end' || phase == 'cancel') && (direction == 'right')) { + console.log('end'); + } + } + + /** + * Browser detect + */ + function isSafari() { + return /Safari/.test(navigator.userAgent) && /Apple Computer/.test(navigator.vendor); + } + + function isChrome() { + return /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor); + } + + /** + * Manually update the position of the nav on drag + */ + function scrollNav(distance, duration) { + nav.css('transition-duration', (duration / 1000).toFixed(1) + 's'); + + if(distance >= 0) { + distance = 0; + } + + if(distance <= navWidth) { + distance = navWidth; + } + + if( distance < 0 ){ + distance = (abs( distance ) / navWidth * 100); + + if( distance > 0 ) + distance = -distance; + } + + if(isSafari() || isChrome()) { + nav.css('-webkit-transform', 'translate(' + distance + '%,0)'); + } + else{ + nav.css('transform', 'translate(' + distance + '%,0)'); + } + if(distance == '0') { + $('.ssm-toggle-nav').addClass('ssm-nav-visible'); + $('html').addClass('is-navOpen'); + $('.ssm-overlay').fadeIn(); + } + } + + /** + * Open / close by click on burger icon + */ + var hideNavigation = (function() { + nav.removeClass('ssm-nav-visible'); + scrollNav(navWidth, settings.speed); + $('html').removeClass('is-navOpen'); + $('.ssm-overlay').fadeOut(); + }); + + var showNavigation = (function() { + nav.addClass('ssm-nav-visible'); + scrollNav(0, settings.speed); + }); + + $('.ssm-toggle-nav').click(function(e) { + if( nav.hasClass('ssm-nav-visible') ){ + hideNavigation(); + } + else{ + showNavigation(); + } + e.preventDefault(); + }); + + var objects = {}; + + // Check if is open + objects['isOpen'] = function(){ + if( nav.hasClass('ssm-nav-visible') ){ + return true; + } + + return false; + } + + // Close (hide) menu + objects['hide'] = function(){ + if( "function" === typeof hideNavigation ){ + hideNavigation(); + } + } + + // Open (show) menu + objects['show'] = function(){ + if( "function" === typeof showNavigation ){ + showNavigation(); + } + } + + // Open/close (hide/show) menu + objects['open'] = function(){ + if( this.isOpen() ){ + this.hide(); + } + else{ + this.show(); + } + } + + return objects; + } + ; })(window.jQuery || window.$, document, window); - /* * Export as a CommonJS module */ if (typeof module !== 'undefined' && module.exports) { - module.exports = slideAndSwipe; + module.exports = slideAndSwipe; } diff --git a/jquery.slideandswipe.min.js b/jquery.slideandswipe.min.js index 27e1ba2..a16e068 100644 --- a/jquery.slideandswipe.min.js +++ b/jquery.slideandswipe.min.js @@ -1 +1,2 @@ -!function(a,b,c,d){"use strict";a.fn.slideAndSwipe=function(b){function g(a,b,g,h){"start"==b&&(e=c.hasClass("ssm-nav-visible")?0:d);var i;"move"==b&&"left"==g?(i=e<0?e-h:-h,j(i,0)):"move"==b&&"right"==g?(i=e<0?e+h:h,j(i,0)):"cancel"==b&&"left"==g&&0===e?j(0,f.speed):"end"==b&&"left"==g?k():"end"!=b&&"cancel"!=b||"right"!=g||console.log("end")}function h(){return/Safari/.test(navigator.userAgent)&&/Apple Computer/.test(navigator.vendor)}function i(){return/Chrome/.test(navigator.userAgent)&&/Google Inc/.test(navigator.vendor)}function j(b,e){c.css("transition-duration",(e/1e3).toFixed(1)+"s"),b>=0&&(b=0),b<=d&&(b=d),h()||i()?c.css("-webkit-transform","translate("+b+"px,0)"):c.css("transform","translate("+b+"px,0)"),"0"==b&&(a(".ssm-toggle-nav").addClass("ssm-nav-visible"),a("html").addClass("is-navOpen"),a(".ssm-overlay").fadeIn())}var c=a(this),d=-c.outerWidth(),e=d,f=a.extend({triggerOnTouchEnd:!0,swipeStatus:g,allowPageScroll:"vertical",threshold:100,excludedElements:"label, button, input, select, textarea, .noSwipe",speed:250},b);c.swipe(f);var k=function(){c.removeClass("ssm-nav-visible"),j(d,f.speed),a("html").removeClass("is-navOpen"),a(".ssm-overlay").fadeOut()},l=function(){c.addClass("ssm-nav-visible"),j(0,f.speed)};a(".ssm-toggle-nav").click(function(a){c.hasClass("ssm-nav-visible")?k():l(),a.preventDefault()})}}(window.jQuery||window.$,document,window),"undefined"!=typeof module&&module.exports&&(module.exports=slideAndSwipe); +/* Copyright 2013-2018 Joan claret | @license MIT | @author Joan Claret Teruel , Lenivene Bezerra */ +(function(c,m,n,p){c.fn.slideAndSwipe=function(a){function k(b,a){d.css("transition-duration",(a/1E3).toFixed(1)+"s");0<=b&&(b=0);b<=f&&(b=f);0>b&&(b=abs(b)/f*100,0g?g-e:-e,k(b,0)):"move"==a&&"right"==c?(b=0>g?g+e:e,k(b,0)):"cancel"==a&&"left"==c&&0===g?k(0,l.speed):"end"==a&&"left"==c?h():"end"!=a&&"cancel"!=a||"right"!=c||console.log("end")},allowPageScroll:"vertical",threshold:100,excludedElements:"label, button, input, select, textarea, .noSwipe",speed:250},a);d.swipe(l);var h=function(){d.removeClass("ssm-nav-visible");k(f,l.speed);c("html").removeClass("is-navOpen");c(".ssm-overlay").fadeOut()},e=function(){d.addClass("ssm-nav-visible");k(0,l.speed)};c(".ssm-toggle-nav").click(function(a){d.hasClass("ssm-nav-visible")?h():e();a.preventDefault()});return{isOpen:function(){return d.hasClass("ssm-nav-visible")?!0:!1},hide:function(){"function"===typeof h&&h()},show:function(){"function"===typeof e&&e()},open:function(){this.isOpen()?this.hide():this.show()}}}})(window.jQuery||window.$,document,window);"undefined"!==typeof module&&module.exports&&(module.exports=slideAndSwipe);