AngularJS:元素跟随光标

时间:2016-01-28 22:15:57

标签: javascript jquery css angularjs

jsfiddle here:https://jsfiddle.net/Flignats/jzrzo56u/3/

我的页面上有一个最初隐藏的元素(popover)。当页面上的另一个元素悬停在上面时,我希望弹出框显示在光标旁边。

在我的小提琴中,我有3个段落和弹出窗口。当用户的光标输入段落时,将显示弹出框。当用户的光标离开元素时,不再显示弹出窗口。

我无法检索光标坐标并将光标位于光标附近。

感谢任何帮助:)

Angular Code:

var app = angular.module('myApp', []);

app.controller('Ctrl',['$scope',function($scope) {
$scope.name = 'Ray';
$scope.popover = false;

//Method to show popover
$scope.showPopover = function() {
return $scope.popover = !$scope.popover;
};


}]);

HTML code:

<div ng-app="myApp" ng-controller="Ctrl">
  <div id="container">
    <p ng-mouseenter="showPopover()" ng-mouseleave="showPopover()">Square 1</p>
    <p ng-mouseenter="showPopover()" ng-mouseleave="showPopover()">Square 2</p>
    <p ng-mouseenter="showPopover()" ng-mouseleave="showPopover()">Square 3</p>
  </div>
  <div class="pop-availability" ng-show="popover">
    <div class="pop-title">
        <p>Title Content Goes Here</p>
    </div>
    <div class="pop-content">
        <table class="pop-table">
            <thead>
                <tr>
                    <th></th>
                    <th ng-repeat='name in data.record'>{{name.name}}</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Row 1</td>
                    <td ng-repeat='available in data.record'>{{available.number}}</td>
                </tr>
                <tr>
                    <td>Row 2</td>
                    <td ng-repeat='unavailable in data.record'>{{unavailable.number}}</td>
                </tr>
                <tr>
                    <td>Row 3</td>
                    <td ng-repeat='unassigned in data.record'>{{unassigned.number}}</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>
</div>

编辑:更新了捕获鼠标坐标的jsfiddle。仍然无法让popover移动到光标:https://jsfiddle.net/Flignats/jzrzo56u/4/

编辑:越来越近,但它有点儿车! https://jsfiddle.net/Flignats/jzrzo56u/5/

解决方案:https://jsfiddle.net/Flignats/jzrzo56u/6/

2 个答案:

答案 0 :(得分:3)

如果你不介意一点jQuery,这看起来会对你有所帮助:http://jsfiddle.net/strangeline/jxqpv/light/

代码:

function getMousePos(evt) {
    var doc = document.documentElement || document.body;
    var pos = {
        x: evt ? evt.pageX : window.event.clientX + doc.scrollLeft - doc.clientLeft,
        y: evt ? evt.pageY : window.event.clientY + doc.scrollTop - doc.clientTop
    };
    return pos;

}
document.onmousemove = moveMouse;

function moveMouse(evt) {
    var pos = getMousePos(evt),
        cood = document.getElementById("showCood");
    cood.style.display = 'block';
    cood.style.left = pos.x + 50 + "px";
    cood.style.top = pos.y + 50 + "px";
    document.getElementById('posX').innerHTML = "X: " + pos.x;
    document.getElementById('posY').innerHTML = "Y: " + pos.y;

}

$(document).mousemove(function(e) {
    $("#jQueryPos").text(e.pageX + "," + e.pageY).show().css({
        'left': e.pageX - 50,
        "top": e.pageY - 50
    })
})

答案 1 :(得分:2)

JSFiddle

HTML - 将$ event参数添加到showPopover函数

<p ng-mouseenter="showPopover($event)" ng-mouseleave="showPopover()">Square 1</p>
<p ng-mouseenter="showPopover($event)" ng-mouseleave="showPopover()">Square 2</p>
<p ng-mouseenter="showPopover($event)" ng-mouseleave="showPopover()">Square 3</p>

并使用ng-style,您可以更改位置Source

<div class="pop-availability" ng-show="popover"
       ng-style="{left:field.left,
                   top:field.top}" > 

JavaScript

//Method to show popover
$scope.showPopover = function(mouseEvent) {
if (!mouseEvent)
  {
    mouseEvent = window.event;
  }
  $scope.field.left = mouseEvent.pageX + 'px';
  $scope.field.top = mouseEvent.pageY+ 'px';
return $scope.popover = !$scope.popover;
};

CSS更改 - 添加了position:absolute

.pop-availability {
    border-radius: 8px;
    box-shadow: 0 0 10px 0 #969696;
    display: inline-block;
    min-width: 375px;
    position:absolute;
}