Tao Wang Tao Wang - 3 months ago 11
Javascript Question

using onmouseup set element position to nearest point not work

To leave out my application scenario for long story short, what I'd like to implement is something like jQuery Slider with anchor points split by equal interval. When I move the cursor and stop at an arbitrary position, there's a method called FindNearestAnchor triggered, and set the cursor position, and move it to the nearest anchor point.

What I've implemented right now is everything except setting cursor position and move it to the nearest anchor. Here is my referenced example.

function _move_elem(e) {
//if set the nearest position here, the cursor will be stuck at the first anchor point.
selected.style.left = nearest[0];
selected.style.top = nearest[1];
}

function _destroy() {
//if set the nearest position here, nothing happened.
selected.style.left = nearest[0];
selected.style.top = nearest[1];



selected = null;
}


Any suggestion will be appreciated, thank you very much!

Answer

I moved these codes into AngularJS directives, and it worked perfectly fine for me.

link:{
    init: function (scope, element, attrs, ngModel) {
          function _move_elem(e) {
                 ...
          }
          function _destroy() {
          }
    }
}