javascript - angularjs - 鼠标按下并在鼠标按下事件时在元素上移动?

标签 javascript angularjs

为了解释我正在尝试做的事情,我创建了一个您可以玩的示例:

http://plnkr.co/edit/usrmiNkj5YJY5SlV8ETw?p=preview

我想在鼠标按下时绘制多个绿色方 block 。 这:

<div ng-mousedown="drawImage($parent.$index,$index)"></div>

只有当鼠标在元素上而不是外部时才有效。

有没有办法检查鼠标是否已经按下并绘制绿色的瓷砖?

请使用我制作的代码来创建一个工作示例。

最佳答案

您必须包含更多的事件处理程序,用于 mouseup 和 mousemove,如下所示

<div class="tile" ng-repeat="x in y track by $index" ng-class="x" ng-mouseup="removeFlag()" ng-mousedown="setFlag($parent.$index,$index)" ng-mousemove="drawImage($parent.$index,$index)"></div>

然后添加函数

$scope.drawImage = function(y,x){
  if ($scope.mouseIsDown)
    $scope.map[y][x] = "green";
}

$scope.setFlag = function(y,x){
   $scope.mouseIsDown = true;
   this.drawImage(y,x)
}

$scope.removeFlag = function(){
   $scope.mouseIsDown = false;
}

这会在鼠标按下时设置一个标志,并在光标移到元素上时设置颜色并且鼠标按下。

PLNKR

关于javascript - angularjs - 鼠标按下并在鼠标按下事件时在元素上移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25223119/

相关文章:

javascript - 使用 Chai、Mocha、Express、johnny-five 和 node 编写测试时出现菜鸟错误

javascript - Internet Explorer 7/8 中的表单验证错误

javascript - Angularjs 打破了 coffeescript 函数表达式

javascript - jQuery 布局插件 + AngularJS 选项卡

angularjs - 如何在 Spring Boot Thymeleaf 中使用 Angular 双向绑定(bind)标签

javascript - 如何使用 json 数据在 d3.js 中创建垂直分组的条形图?

javascript - 字符串引用的对象未显示

javascript - Mobile Safari 有时不会触发点击事件

angularjs 弹出窗口不适用于路由模板和 Controller

javascript - $scope.$watch 函数未按预期触发 nodelist.length 更改