Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New functions and responsive #19

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,19 @@ Default initialization
```javascript
<script>
$(document).ready(function() {
$('nav').slideAndSwipe();
var navigation = $('nav').slideAndSwipe();

// Check if is open
navigation.isOpen(); // return boolean

// Close (hide) menu
navigation.hide();

// Open (show) menu
navigation.show();

// Open/close (hide/show) menu
navigation.open();
});
</script>
```
Expand Down
319 changes: 188 additions & 131 deletions jquery.slideandswipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @copyright Copyright 2013-2015 Joan claret
* @license MIT
* @author Joan Claret Teruel <dpam23 at gmail dot com>
* @author Joan Claret Teruel <dpam23 at gmail dot com> | Lenivene Bezerra <lenivene at soufleet dot com>
*
* Licensed under The MIT License (MIT).
* Copyright (c) Joan Claret Teruel <dpam23 at gmail dot com>
Expand All @@ -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;
}
3 changes: 2 additions & 1 deletion jquery.slideandswipe.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.