javascript - 如何在 jQuery-Mobile Datepicker Widget 中通过滑动更改月份

标签 javascript jquery jquery-mobile datepicker jquery-ui-datepicker

我尝试在我的 jQuery-Mobile 项目中实现 DatePicker。这是来源:http://demos.jquerymobile.com/1.4.1/datepicker/

不幸的是,它默认不支持通过滑动事件更改月份。我确实得到了这个question (可能)与我有相同的情况,但不知何故它不适用于我的情况。

这是我尝试过的:

$('#ui-datepicker-div').on("swipeleft", function () {
    console.log("left");
    var thedate = $("#ui-datepicker-div").date('getDate'); //get date from datepicker ( I can't get anything from here)
    //thedate.setMonth(thedate.getMonth() + 1); //add a month 
    //$(this).datepicker('setDate', thedate); // set the date in datepicker
});

有人知道吗?

最佳答案

我通过向我的日期选择器容器添加 swipeleftswiperight 事件处理程序来做到这一点:

  $("#datepicker").on("swipeleft", function(){
    $("#datepicker table").first().animate({
      marginLeft: "-100%"
    },{
      duration: "fast",
      complete: function () {
        $.datepicker._adjustDate("#datepicker", +1, "M" );
      }
    });
  });
  $("#datepicker").on("swiperight", function(){
    $("#datepicker table").first().animate({
      marginLeft: "100%"
    },{
      duration: "fast",
      complete: function () {
        $.datepicker._adjustDate("#datepicker", -1, "M" );
      }
    });
  });

这两个事件都记录在此处:swipeleft在这里:swiperight .

此外,我刚刚使用了SO How to disable text selection using jQuery? 的答案避免在我的日期选择器日历中选择文本(来源:Damien):

  $(".ui-datepicker").attr('unselectable','on')
     .css({'-moz-user-select':'-moz-none',
           '-moz-user-select':'none',
           '-o-user-select':'none',
           '-khtml-user-select':'none',
           '-webkit-user-select':'none',
           '-ms-user-select':'none',
           'user-select':'none'
     }).bind('selectstart', function(){ return false; });

由于您没有具体说明为什么您提到的答案对您的案例不起作用,请随时检查我的 Plunker demo也在真实的移动设备中,让我知道这现在是否适合您。

编辑: 我做了一个小更改,以避免选择日期选择器按钮时出现令人讨厌的拖动副作用:

<div id="datepicker" ondragstart="return false;"></div>

现在它运行良好且顺利!

鸣谢:SyntaxError来自这个答案:Disable Drag and Drop on HTML elements?

关于javascript - 如何在 jQuery-Mobile Datepicker Widget 中通过滑动更改月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41854174/

相关文章:

javascript - 从对象数组到数组对象

javascript - .replace 不适用于 xmlhttp.responseText - Javascript

javascript - 使用在 IE 和 firefox 中不起作用的 Jquery 动态更改图像 src

jquery - 侧边栏在悬停时更改类别背景颜色(jsFiddle)

javascript - 如何为 Notification.requestPermission() 函数选择正确的回调?

javascript - react : What happens on consecutive renders where a list shuffles?

jQuery 在 $(html_with_inline_styles) 上丢失样式

jquery - 使用 jQuery Mobile,我如何检测用户每次查看数据角色 ="page"

javascript - jquery mobile listview 对过滤后的项目进行计数

javascript - JqueryMobile 1.4.3。图标没有出现?