简体   繁体   English

你可以通过动态改变不透明度来定位元素吗?

[英]Can you target an element by its dynamically changing opacity?

Through the use of a function in jQuery, along with my HTML & CSS, I have a series of different colored divs that change their opacity to appear as though the opaque div moves from left to right. 通过在jQuery中使用一个函数,以及我的HTML和CSS,我有一系列不同颜色的div,它们改变了它们的不透明度,就好像不透明的div从左向右移动一样。 I want the user to be able to click a red button to stop the animation on a square of his/her choosing. 我希望用户能够单击红色按钮以停止他/她选择的方格上的动画。 Right now I can get the animation to stop (albeit after it finishes its queued animations), but I am having trouble getting the square that has its opacity at 1 (at the time of the button click) stay at opacity 1. Any help would be greatly appreciated. 现在我可以让动画停止(虽然它完成了排队的动画之后),但是我无法将其不透明度为1的广场(在按钮点击时)保持不透明度1.任何帮助都会非常感谢。

Here is a jsfiddle http://jsfiddle.net/seifs4/krm6uenj/ 这是一个jsfiddle http://jsfiddle.net/seifs4/krm6uenj/

$(document).ready(function () {

$.fn.extend({
    brighten: function(){
        $(this).fadeTo(150, 1);
    }
});
$.fn.extend({
    fade: function(){
        $(this).fadeTo(150, 0.2);
    }
});

function animateSequence() {
    $('.game-square').each(function (i) {
        $(this).delay((i++) * 145).brighten();
        $(this).delay((i++) * 5).fade();
    });
}
animateSequence()
var interval=setInterval(animateSequence, 1700);

$('#red-button').click(function(){

    $('.game-square').each(function(){
        if ($('.game-square', this).not().css('opacity') == 0.2){
        $(this).css('opacity', '1');
        }
    });
    clearInterval(interval);
});

}); });

You maybe need something like this: 你可能需要这样的东西:

function animateSequence(){
    this.current = 0;
    this.squares = $(".game-square");
    this.animate = function(){
        this.squares.eq(this.current).fadeTo(150, 1, function(){
            $(this).fadeTo(150, 0.2)
        });
        this.current = this.current >= this.squares.length - 1 ? 0 : this.current + 1;
    };
    this.start = function(){
        this.running = setInterval(this.animate.bind(this), 150)    
    };
    this.stop = function(){
        this.running = clearInterval(this.running);            
        this.squares.eq(this.current).stop().css("opacity",1);
        alert("Current color: " + this.squares.eq(this.current).attr("class"))
    }
}

Demo 演示

This is the advantage of working with objects, a way very readable, simple and orderly. 这是使用对象的优势,这种方式非常易读,简单有序。

The JQuery .stop() function help you to stop the animation. JQuery .stop()函数可以帮助您停止动画。 I know this is not the best solution for your problem because your opacity stay "1" only a short time. 我知道这不是解决您问题的最佳方法,因为您的不透明度只会在短时间内保持“1”。

    $('#red-button').click(function(){
      clearInterval(interval);
      $('.game-square').stop();//this stop the animation
      $('.game-square').each(function(){
        if ($(this).not().css('opacity') > '0.2'){// I changed this logic
            $(this).css('opacity', '1');
        }
      });
    });

I will take a different and less complex approach. 我会采取一种不同的,不那么复杂的方法。 Perhaps it has even better performance. 也许它有更好的表现。

Demo http://jsfiddle.net/LkatLkz2/8/ 演示 http://jsfiddle.net/LkatLkz2/8/

This is the whole code. 这是整个代码。 I use css for animation effect, and class changing opacity. 我使用css进行动画效果,并使用类更改不透明度。

var sqrs = $('.game-square'),
    len = sqrs.length,
    i=0,
    looping = true;

setInterval(function(){
    if (!looping) return;
    sqrs.removeClass('full').eq(i).addClass('full');
    i = ++i % len;
},400);


$("#red-button").click(function () {
    looping = !looping;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM