简体   繁体   English

在Raphael.js中,控制悬停时元素的顺序

[英]In Raphael.js, controlling order of elements on hover

I have the following code, and here's a fiddle . 我有以下代码, 这是一个小提琴 I'm trying to get the topmost circle to go to the front when it's animated last, ie when the user goes over the mouseover areas starting from the bottom. 我正在尝试使最后一个动画最顶部的圆圈位于最前面,即当用户从底部开始移到鼠标悬停区域时。 In my head, this code should work due to the moboxonoff variable, but it doesn't seem to have any effect. 在我看来,该代码应该由于moboxonoff变量而起作用,但似乎没有任何效果。

Edit for clarity: right now, the bottom circle is always on top of the top circle. 为清楚起见进行编辑:现在,底部的圆圈始终在顶部的圆圈上方。 I'd like this order to change depending on what order the circles' controlling mouseover areas are moused over. 我希望此顺序可以更改,具体取决于将圆形控制鼠标悬停区域滑过的顺序。

xValues = [50, 50];
yValues = [100, 120];

var mouseoverStarts = [70, 90, 130],
    mouseoverEnds =  [90, 130, 150],
    mouseoverItems =  [[0], [0, 1], [1]];

var p0 = Raphael("paper", 500, 500);

var circles = []
for (i = 0; i < 2; i++) {
    circles[i] = p0.circle(xValues[i], yValues[i], 10);
    circles[i].attr({"fill": "#c00", stroke: "#888", "stroke-width":1.5});
    circles[i].id = "c"+i;
};

animate_on = function(num, ignoreindex, ignorearray) {
        if (moboxonoff[num] === 0) {
            p0.getById("c"+num).stop().animate({r: 30}, 480, "bounce");
            p0.getById("c"+num).toFront();
            moboxonoff[num] = 1
        };
};
animate_off = function(num, ignoreindex, ignorearray) {
        if (moboxonoff[num] == 1) {
            p0.getById("c"+num).stop().animate({r: 10}, 320, "bounce");
            moboxonoff[num] = 0
        };
};

moboxonoff = [];
for (i=0; i<2; i++) {
    moboxonoff[i] = 0;
};

debugopacity = 0.2; // set to 0 for final

moboxes = [];
for (i=0; i<mouseoverStarts.length; i++) {
    moboxes[i] = p0.rect(30, mouseoverStarts[i], 400, mouseoverEnds[i] - mouseoverStarts[i]);
    moboxes[i].attr({fill: "#00f", opacity: debugopacity});
    moboxes[i].id = "mob" + i;
    moboxes[i].hover(function () {
        j = parseInt(this.id.slice(3));
        mouseoverItems[j].forEach(animate_on);
        this.toFront();
        }, function () {
        mouseoverItems[j].forEach(animate_off);
        });
};

Change the middle rectangle to the last position in the first 3 arrays: 将中间矩形更改为前三个数组中的最后一个位置:

var mouseoverStarts = [70, 130, 90],
    mouseoverEnds =  [90, 150, 130],
    mouseoverItems =  [[0], [1], [0, 1]];

Remove the toFront() call on the animate functions 删除动画函数上的toFront()调用

And just add a if condition in the mouseover function 只需在mouseover函数中添加if条件

if(j!=2){
  circles[j].toFront();
}

Also you can call the circles from the array directly, no need of creating another ID. 您也可以直接从数组中调用圆圈,而无需创建另一个ID。

http://jsfiddle.net/crockz/u2zL29jw/ http://jsfiddle.net/crockz/u2zL29jw/

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

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