区分点击和保持&单击

时间:2017-08-07 23:52:04

标签: angularjs

我有一个可以拖放的html元素&也可以点击进行另一个动作,我想区分这两个动作,所以我想我会使用ng-mousedown& ng-mouseup并计算它们之间的时间差,并根据我能够判断这是点击还是点击并按住(又称拖放)。

所以元素看起来像这样: <a ng-mousedown="mouseDownStudent()" ng-mouseup="mouseUpStudent()"> {{student.name}} </a>

我想做的事情就是:

        var isMouseDown = false;
        var clickAndHoldTime = 0;
        $scope.mouseDownStudent = function(){
            isMouseDown = true;
            while(isMouseDown){
                clickAndHoldTime++;
            }
        }

        $scope.mouseUpStudent = function(){
            isMouseDown = false;
            // If clickAndHoldTime > 100 ... it's a click
        }

当然这不起作用,而while循环不会停止,我考虑使用$interval,但不确定它在这种情况下是如何适合的。

1 个答案:

答案 0 :(得分:1)

您的实施非常接近。你想要做的是捕获mousedown上的当前时间,然后在mouseup上,找到经过的时间。您可以通过捕获当前日期然后在鼠标放置后找到差异来完成此操作。

public function create() {
    if (!filter_has_var(INPUT_POST, 'name') ||
            !filter_has_var(INPUT_POST, 'type') ||
            !filter_has_var(INPUT_POST, 'rent_cost') ||
            !filter_has_var(INPUT_POST, 'rental_duration') ||
            !filter_has_var(INPUT_POST, 'level') ||
            !filter_has_var(INPUT_POST, 'image') ||
            !filter_has_var(INPUT_POST, 'description')) {

        return false;
    }

    $name = filter_input(INPUT_POST, "name", FILTER_SANITIZE_STRING);
    $type = filter_input(INPUT_POST, "type", FILTER_SANITIZE_STRING);
    $rent_cost = filter_input(INPUT_POST, "rent_cost", FILTER_SANITIZE_STRING);
    $rental_duration = filter_input(INPUT_POST, "rental_duration", FILTER_SANITIZE_STRING);
    $level = filter_input(INPUT_POST, "level", FILTER_SANITIZE_STRING);
    $image = filter_input(INPUT_POST, "image", FILTER_SANITIZE_STRING);
    $description = filter_input(INPUT_POST, "description", FILTER_SANITIZE_STRING);

    try {
        if (!is_numeric($rent_cost OR $rental_duration OR $level)) {
            throw new DataTypeException(gettype($rent_cost OR $rental_duration OR $level), "number");
        }
        if ($name OR $type OR $rent_cost OR $rental_duration OR $level OR $image OR $description == "") {
            throw new RequiredValueException("Fatal Error: <br> Please complete all fields");
        }

        $new_rentalpokemon = new Rentalpokemon($name, $type, $rent_cost, $rental_duration, $level, $image, $description);
        $this->rentalpokemon_model->add_rentalpokemon($new_rentalpokemon);

        $view = new RentalpokemonDetail();
        $view->display($new_rentalpokemon);
    } catch (RequiredValueException $ex) {
        echo $message = $ex->getMessage();
    } catch (DataTypeException $ex) {
        echo $message = $ex->getMessage();
    } catch (Exception $ex) {
        echo $message = $ex->getMessage();
    }