简体   繁体   English

jQuery在手机上向左或向右滑动

[英]Jquery swipe left or right on mobile

I want to swipe left or right using jquery mobile. 我想使用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. 目前,我按下上面的按钮,菜单功能用于打开菜单,我想通过在jquery中使用滑动功能来做到这一点。 Please help 请帮忙

Html Divs: 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: JavaScript函数:

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

Very old question, but it can help others who want a simple code to get swap event in JavaScript without any dependency. 这个问题很老,但是它可以帮助那些想要简单代码的人在没有任何依赖的情况下在JavaScript中获取交换事件。 Hope it will help others. 希望它能帮助别人。

See script at swipe.js swipe.js上查看脚本

simple to use in html markup : 易于在html标记中使用:

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

Use in Javascript : 在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 : //vikylp.github.io/swipe/

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

https://api.jquerymobile.com/swiperight/ 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();
} );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM