admin管理员组

文章数量:1277901

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();
    });

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();
    });
Share Improve this question edited Oct 26, 2015 at 7:09 user3742031 asked Oct 26, 2015 at 6:12 Passionate ProgrammerPassionate Programmer 1741 gold badge2 silver badges17 bronze badges 1
  • Better if you can add a fiddle. – Deshan Commented Oct 26, 2015 at 6:26
Add a ment  | 

2 Answers 2

Reset to default 5

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/

https://api.jquerymobile./swipeleft/

https://api.jquerymobile./swiperight/

Swipe Left Event:

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

Swipe Right Event:

jQuery( window ).on( "swiperight", function( event ) 
{
   ShowMenu();
} );

本文标签: javascriptJquery swipe left or right on mobileStack Overflow