简体   繁体   English

jQuery animate()不透明度

[英]jQuery animate() opacity

I'm creating a sketchpad where a mouseover creates an effect on a grid of divs. 我正在创建一个画板,将鼠标悬停在div网格上会产生效果。 The effect depends on the radio button selected. 效果取决于所选的单选按钮。 There's probably a better way to do this with CSS, but the point of building this was to practice Javascript and jQuery, so that's how I'm trying to do the effects. 使用CSS可能有更好的方法,但是构建此代码的目的是练习Javascript和jQuery,所以这就是我试图做到的效果。

Code: https://jsfiddle.net/xc3w1z9m/ 代码: https//jsfiddle.net/xc3w1z9m/

Full screen view: https://jsfiddle.net/xc3w1z9m/embedded/result/ 全屏视图: https : //jsfiddle.net/xc3w1z9m/embedded/result/

Problem 1) Each effect works as expected except for the "Trail" effect. 问题1)除“足迹”效果外,每种效果均按预期方式工作。 The "Trail" effect is supposed to fade in the div to full opacity upon "mouseover", and then fade it out to transparent on "mouseout". “拖尾”效果应该在“鼠标悬停”时在div中逐渐消失为完全不透明,然后在“鼠标悬停”时使其逐渐消失为透明。 My code is a bit of a cluster right now, because of constant adjustments, but the event handlers for "mouseover" and "mouseout" are in place. 由于不断进行调整,我的代码现在有点集群,但是“ mouseover”和“ mouseout”的事件处理程序已经到位。 The "mouseout" event handler isn't working. “ mouseout”事件处理程序不起作用。

The placement of the following event handler seems to effect whether it works. 以下事件处理程序的放置似乎会影响它是否起作用。

$('grid-box').on('mouseout', function(){
    $(this).animate({
    opacity: 0
    }, 500);
});

If I place it where it is now, it doesn't work. 如果我将其放在现在的位置,它将无法正常工作。 If I place it on line 27 (within the other event handler), it does work. 如果我将其放在第27行(在另一个事件处理程序中),它确实可以工作。 I'm hesitant to do this because I'm not sure if it's a good idea to have an event handler within an event handler. 我很犹豫,因为我不确定在事件处理程序中是否有一个事件处理程序是否是一个好主意。

Any ideas? 有任何想法吗?

Problem 2) This problem has to do with the divs sometimes not covering the full height of the grid. 问题2)此问题与div有时不能覆盖网格的整个高度有关。 For example, press "Reset Grid" and set it to 100x100 (enter 100). 例如,按“重置网格”并将其设置为100x100(输入100)。 The boxes don't fill up the entire height of the grid, but by my calculations it should. 这些盒子并不能填满整个网格的高度,但是根据我的计算,它应该可以。 There's also a little space left at the bottom and the sides sometimes for other entries, such as 17. Most grid sizes below 40x40 fill up the entire height and width. 底部和侧面有时还会留出一些空间,用于其他条目,例如17。大多数40x40以下的网格尺寸会填满整个高度和宽度。

I'm using the following calculations: 我正在使用以下计算:

  • Determine the size of each individual div by dividing the total size of the grid by the number of divs per row/height. 通过将网格的总大小除以每行/高度的div数来确定每个div的大小。 The total size of the grid is 480x480px, so if the user enters 20, the calculation would be 480/20, and each box should be 24x24px. 网格的总大小为480x480px,因此,如果用户输入20,则计算将为480/20,每个框应为24x24px。
  • Determine the number of individual divs needed by multiplying the number of boxes needed per row and height. 通过乘以每行和高度所需的框数来确定所需的单个div数。 If the user wants a 20x20 grid, 400 divs should be generated. 如果用户希望使用20x20的网格,则应生成400格。

Help with either problem would be much appreciated! 对任何一个问题的帮助将不胜感激!

Problem 1) Try this https://jsfiddle.net/xcnwtnt2/1/ 问题1)试试这个https://jsfiddle.net/xcnwtnt2/1/

            case 'trail':
                var e = $(this);
                $(e).stop();
                $(this).animate({
                    opacity: 1
                },200);
                $(this).mouseout(function(_e) {
                    $(e).animate({
                        opacity: 0
                    },200);
                });
                break;

Problem 2) 问题2)

You are getting rounding errors, I think: eg 480/100 = 4 in integer, leaving 80 behind. 我认为您正在舍入错误:例如480/100 = 4(整数),而后面则保留80。 You need to figure out a better system of dividing the blocks. 您需要找出一个更好的划分块的系统。

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

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