I recently made the switch from using onclick on a div to touchstart in order to increase the speed of clicking on a touch screen. However, I have encountered an issue where the CSS property that changes the background color is no longer functioning. Below is the code snippet:
<div class="numpad" id="numpad" align="center">
<div style="display:flex;">
<kbd touchpad="call(1)">1
<div class="background"> </div>
</kbd>
<kbd touchpad="call(2)">2
<div class="background">ABC</div>
</kbd>
<kbd touchpad="call(3)">3
<div class="background">DEF</div>
</kbd>
</div>
Here's the Angular directive:
angular.module('myApp').directive('numpad', function($log) {
return {
restrict: 'E',
templateUrl: 'html/directives/numpadOpenr.html',
require: 'ngModel',
link: function(scope, elem, attrs, ngModel) {
scope.number = ngModel;
scope.call = function(number) {
var value = scope.number.$viewValue || '';
scope.number.$setViewValue(value + number);
};
scope.remove = function() {
var value = scope.number.$viewValue || '';
if (value.length > 0) {
scope.number.$setViewValue(value.substring(0, value.length - 1));
}
};
}
};
});
angular.module('myApp').directive("touchpad", [function () {
return function (scope, elem, attrs) {
elem.bind("touchstart click", function (e) {
e.preventDefault();
e.stopPropagation();
scope.$apply(attrs["touchpad"]);
});
}
}]);
And here is the CSS:
kbd, .key {
display: inline-block;
min-width: 2.2em;
font: normal .85em "Lucida Grande", Lucida, Arial, sans-serif;
text-align: center;
text-decoration: none;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
-ms-border-radius: 3px;
-o-border-radius: 3px;
border-radius: 3px;
cursor: pointer;
color: #555;
}
kbd[title], .key[title] {
cursor: help;
}
kbd {
border: 1px solid #c4c6ca;
padding: 10px 20px;
background-color: transparent;
flex:1;
height: 2.2em;
}
kbd:active {
background: #e6e7e8;
}
.numpad kbd {
margin: 3px;
font-size: xx-large;
background-color: #ffffff;
}
kbd div.background {
font-size:x-small;
}
.numpad kbd:hover, .numpad kbd:active, .numpad kbd:focus {
background-color: #dceef7;
}
Is there a way to ensure that the CSS properties for numpad kbd:active and numpad kbd:focus continue to work while using the same strategy with e.preventDefault() and e.stopPropagation()? Any insights would be greatly appreciated.