【问题标题】:Disable / Stop / Prevent @click event on image element using condition - vuejs使用条件禁用/停止/防止图像元素上的@click事件 - vuejs
【发布时间】:2022-01-23 14:26:29
【问题描述】:

我有以下代码:

<image
    id="wheel-bg"
    class="wheel-bg"
    :x="-width/2"
    :y="-height/2"
    href="images/wheel-bg.png"
    :width="width"
    :height="height"
    filter="drop-shadow(black 0px 0px 1rem)"
    @click="spin"
></image>

我有一个数据属性“rotationState”,它可以是“false”或“true”。

rotationState = false 表示@click 事件应该触发并且

rotationState = true 表示@click 事件不应触发

我试过了

@click="spin && rotationState"

我也试过了:

@click.stop="rotationState"
@click="spin"

和

:disabled="rotationState"

但上述方法不起作用,因为我认为我在 &lt;image&gt;&lt;/image&gt; 元素上有 @click 事件。

我想要达到的基本上是,

container.on('click', null);这是我第一次用jquery/js写代码时用到的代码。

Jquery Spin 功能代码

container.on("click", spin); //this code triggers spin in jquery

function spin(d) {

    container.on("click", null); //this code disables multiple spin from happening

    spinCount++;

    // First 2 spins
    if (spinCount < totalSpins) {

        //get random number of rotations + get random degree for "No Win"
        // 1 to 28 or 211 to 239 deg
        if (Math.random() < 0.5) {
            rotation = ((Math.floor(Math.random() * 1) + 3) * 360) + (Math.floor(Math.random() * 28) + 1);
        } else {
            rotation = ((Math.floor(Math.random() * 1) + 3) * 360) + (Math.floor(Math.random() * 28) + 211);
        }
        duration = 10000; // 10 seconds

        // Third spin
    } else if (spinCount == totalSpins) {

        //get random number of rotations + get random degree for options other than "No Win" and "Free Apple Earpods"
        //31 to 119 deg or 151 to 209 deg or 271 to 359
        if (Math.random() < 0.7) {
            rotation = ((Math.floor(Math.random() * 1) + 5) * 360) + (Math.floor(Math.random() * 88) + 31);
        } else if (Math.random() > 0.3 && Math.random() <= 0.7) {
            rotation = ((Math.floor(Math.random() * 1) + 5) * 360) + (Math.floor(Math.random() * 58) + 151);
        } else {
            rotation = ((Math.floor(Math.random() * 1) + 5) * 360) + (Math.floor(Math.random() * 88) + 271);
        }
        duration = 15000; // 15 seconds

        // Spins more than 3
    } else {

        alert("No more spins left");
        container.on("click", null);
        return;
    }

    updateAttempts(spinCount);
    updateSpins(totalSpins - spinCount);

    //Get selected array element
    var degSelected = (rotation % 360) % 360;

    if (
        (degSelected >= 91 && degSelected <= 120) ||
        (degSelected >= 181 && degSelected <= 210) ||
        (degSelected >= 331 && degSelected <= 360)
    ) {
        selected = 11;

    } else if (
        (degSelected >= 1 && degSelected <= 30) ||
        (degSelected >= 121 && degSelected <= 150) ||
        (degSelected >= 181 && degSelected <= 210) ||
        (degSelected >= 211 && degSelected <= 240)
    ) {
        selected = 10;

    }else if (
        (degSelected >= 31 && degSelected <= 60) ||
        (degSelected >= 151 && degSelected <= 180) ||
        (degSelected >= 271 && degSelected <= 300)
    ) {
        selected = 9;

    }else if (degSelected >= 241 && degSelected <= 270) {

        selected = 6;

    }else if (
        (degSelected >= 61 && degSelected <= 90) ||
        (degSelected >= 301 && degSelected <= 330)
    ) {
        selected = 4;
    }

    vis.transition()
        .duration(duration)
        .ease(d3.easeExpOut)
        .attrTween("transform", rotTween)
        .each("end", function () {

            //set prize value
            prize = data[selected].value;
            id = data[selected].id;

            if(spinCount == 3) {
                alert('Your Voucher: ' + data[selected].label);
            }else {
                alert('Better luck next time');
            }

            oldrotation = rotation;
            container.on("click", spin);
        });

    img2.transition()
        .duration(duration)
        .ease(d3.easeExpOut)
        .attrTween("transform", rotTween)
   
}

【问题讨论】:

  • 试试@click.native ?首先你需要检查你的点击处理程序是否被触发,然后设置一个标志来判断你是否可以继续处理程序的操作
  • @fengxh error 'v-on' 指令上的 '.native' 修饰符已弃用。 .native 不起作用我想我正在使用 vuejs 3
  • @fengxh rotationState 是数据属性,它在饼轮旋转时设置标志,并且在饼轮停止旋转时更新标志..
  • 那么是否触发了点击处理程序?
  • @fengxh 是的,如果我有代码 @click="spin",则会触发单击处理程序,但是当我放置 && 条件时,单击处理程序不会触发。我已经更新了代码,请检查 jquery 中使用的代码,该代码在饼图轮旋转时阻止了点击。 container.on('click', null);

标签: vue.js vuejs3


【解决方案1】:

您可以根据前面提到的rotationState的状态设置@click的功能:

 @click="rotationState ? spin() : null"

【讨论】:

    猜你喜欢
    • 2013-09-29
    • 1970-01-01
    • 2012-11-05
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多