Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery swipe left or right on mobile

I want to swipe left or right using jquery mobile. When I click the swipe left it opens the drawer and on right it closes the drawer. Currently I press the upper button and menu function is used to open menu I want to do it by using swipe function in jquery. Please help

Html Divs:

<div class="DinM-navbar-header ">
        <img src="" data-target=".DinM-navbar-collapse" data-toggle="collapse" class="DinM-navbar-toggle" alt="menu-icon" />
    </div>
    <div class="">
        <%=HTML %>
    </div>

Javascript function:

 function ShowMenu() {
            var mainmenu = $(".DinM-main-menu");
            if (($(window).width() <= 991 && mainmenu.parent().attr('id') != "mobile")) {
                $("#main-wrapper").animate({
                    left: "0px"
                });
                $("#mobile").html(mainmenu.clone());
                $(".DinM-main-menu", $(".DinM-top-menu")).remove();
                $(".DinM-main-menu").css("z-index", "-1").hide();
                $("#main-wrapper").css("min-height", $(".DinM-main-menu").height());
            } else if ($(window).width() > 992 && mainmenu.parent().attr('id') == "mobile") {
                mainmenu.insertAfter($(".DinM-navbar-header "));
                mainmenu.show();
                $("#mobile").html('');
            }

 $(document).ready(function () {
        $("body").wrapInner("<div id='main-wrapper' class='content-left' ></div>").append("<div id='mobile' ></div>");
        $(".DinM-main-menu").removeClass('DinM-hide');
        ShowMenu();
    });
like image 326
Passionate Programmer Avatar asked Oct 16 '25 14:10

Passionate Programmer


2 Answers

Very old question, but it can help others who want a simple code to get swap event in JavaScript without any dependency. Hope it will help others.

See script at swipe.js

simple to use in html markup :

<div id="touchsurface1" class="touchsurface" onSwap="touchsurfaceSwap(event)"></div>

Use in Javascript :

touchsurface = document.getElementById('touchsurface2');
touchsurface.addEventListener("swap", function(event){alert('Swaped ' + event.detail.direction + ' at ' + event.target.id);}, false);

Demo page : https://vikylp.github.io/swipe/

like image 72
Vikram Mali Avatar answered Oct 18 '25 06:10

Vikram Mali


https://api.jquerymobile.com/swipeleft/

https://api.jquerymobile.com/swiperight/

Swipe Left Event:

  jQuery( window ).on( "swipeleft", function( event ) 
  {
    hideMenu();
  } );

Swipe Right Event:

jQuery( window ).on( "swiperight", function( event ) 
{
   ShowMenu();
} );
like image 21
Deshan Avatar answered Oct 18 '25 07:10

Deshan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!