【问题标题】:Add an active class just in mobile仅在移动设备中添加活动课程
【发布时间】:2019-02-27 19:32:38
【问题描述】:

我创建了一个响应式搜索按钮,一旦用户单击它就会打开一个表单,但在移动设备中,该表单应该会出现,而无需用户单击任何按钮。

我可以通过添加一个类来做到这一点,所以第一次单击按钮打开菜单,第二次单击它将提交输入。

现在的障碍是在移动设备中添加活动类。最好的方法是什么?如果 userAgent 检测到具有更大屏幕的移动设备(如平板电脑)。

我知道可以通过检测设备的屏幕尺寸来实现这一点,问题是什么对我的目的更有效,或者是否有任何其他方式可以实现这一目标。

以下是代码的相关部分:

$(document).on('click', '.navbar-collapse form[role="search"]:not(.active) button[type="submit"]', function(event) {
  event.preventDefault();
  var $form = $(this).closest('form'),
  $input = $form.find('input');
  $form.addClass('active');
  $input.focus();
});

【问题讨论】:

标签: javascript jquery mobile


【解决方案1】:
if($(window).width()<768){
    $("body").addClass("mobileview");
} 
else {
    $("body").removeClass("mobileview");
}
// this is used whenever the window is resized
$(window).resize(function(){
  if($(window).width()<768){
        $("body").addClass("mobileview");
    } 
    else {
        $("body").removeClass("mobileview");
    }
});

【讨论】:

    【解决方案2】:

    您可以检查用户是否正在使用这样的移动设备:

    if( navigator.userAgent.match(/Android/i)
     || navigator.userAgent.match(/webOS/i)
     || navigator.userAgent.match(/iPhone/i)
     || navigator.userAgent.match(/iPad/i)
     || navigator.userAgent.match(/iPod/i)
     || navigator.userAgent.match(/BlackBerry/i)
     ){
       $form.addClass('active');
    
     // some code..
    }
    

    【讨论】:

    • 我编辑了我的问题。有些带有这些 userAgent 的移动设备具有更大的屏幕,所以我正在寻找一个同样基于设备屏幕尺寸的灵魂。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    • 2019-04-21
    • 1970-01-01
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多