如何仅在移动设备上消除输入的焦点

How do remove focus from inputs only on mobile devices

本文关键字:输入 焦点 何仅 移动      更新时间:2023-09-26

我正在开发中/大型angularjs应用程序。根据用户来自移动设备还是桌面设备,我有不同风格的视图加载。

我的问题是使用角度设置的自动对焦。在桌面上,这是有益的,而在移动设备上,这则是糟糕的用户体验。如何删除仅在移动设备上对输入字段的自动聚焦。

我正在使用简单的if-else语句来指定要加载的视图。加载移动视图时,是否有方法将焦点设置为false?

//Detect Mobile Switch Refill List To Grid
if(window.innerWidth <= 800) {
    $scope.view = "MobileAccount";
} else {
    $scope.view = "DesktopAccount";
}

解决了它。只需要在focus指令中将window.innerWidth添加到我的if语句中。

angular.module('App.Directives').directive('focus', function($timeout) {
return {
    scope: {
        trigger: '@focus'
    },
    link: function(scope, element) {
        scope.$watch('trigger', function(value) {
            if (value === "true" && window.innerWidth >= 800) {
                $timeout(function() {
                    element[0].focus();
                });
            }
        });
    }
};
});