最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS - How to store mouse coordinates on mousemove? - Stack Overflow

programmeradmin2浏览0评论

Trying to translate something I had working without angular to work with angular, and it seems I understand angular less clearly than I thought:

I have a number of static divs and one movable div that follows the mouse. If the user clicks while the movable div is overlapping a static div, it triggers an event (currently a Bootstrap modal). Currently, it also calculates the distance of the mouse and the static divs and changes the background color based on distance (i.e. closer to the red div, background is brighter red, closer to blue div, background brighter blue, between the two, the colors fade/blend from red to purple to blue, etc).

Currently, I am just trying to capture mouse position when the mouse moves. The next object is to find the centerpoint of each static div.

Trying to translate something I had working without angular to work with angular, and it seems I understand angular less clearly than I thought:

I have a number of static divs and one movable div that follows the mouse. If the user clicks while the movable div is overlapping a static div, it triggers an event (currently a Bootstrap modal). Currently, it also calculates the distance of the mouse and the static divs and changes the background color based on distance (i.e. closer to the red div, background is brighter red, closer to blue div, background brighter blue, between the two, the colors fade/blend from red to purple to blue, etc).

Currently, I am just trying to capture mouse position when the mouse moves. The next object is to find the centerpoint of each static div.

Share Improve this question asked May 10, 2016 at 23:54 Nick B.Nick B. 631 silver badge8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

You can use the ng-mousemove directive and pass the mouse event to your angular controller.

See this plnkr for an example.

View

<div class="big" ng-controller="TestCtrl" ng-mousemove="captureCoordinate($event)">
    <div>x: {{x}}</div>
    <div>y: {{y}}</div>
</div>

Controller

   $scope.captureCoordinate = function($event){
      $scope.x = $event.x;
      $scope.y = $event.y;
   }

In tandem with the ng-mousemove directive, you can pass the $event object to your controller function and grab x and y coordinates off of there.

Maybe something like:

angular.directive('mouseMoveDirective', mouseMoveDirective);
function mouseMoveDirective() {
  return {
    link: link
  }

  function link(scope, element, attrs) {
    $(element).mousemove(function(e) {
      var pageCoords = (e.pageX + ":" + e.pageY);
    })
  }
}

Benefit of using directive is that its reusable.

发布评论

评论列表(0)

  1. 暂无评论