2

你好

所以我开始写一些函数,女巫会这样做(你越靠近你将鼠标移动到 div 越靠近 div 移动到鼠标位置on parent X axsis,最大 div 位置是left:-40%,最小值是left: -80%):

注:黑色箭头为光标(鼠标位置)

  1. 项目清单
  2. 项目清单
  3. 项目清单

代码

HTML标记:

<div class="wraper">
    <div class="ls">
        <div class="ldiv">
        </div>
    </div>
    <div class="c">
        <div class="coordinates">
        </div>
        <div class="cdiv">
        </div>
    </div>
    <div class="rs">
        <div class="rdiv">
        </div>
    </div>
</div>

CSS标记:

body{
    margin: 0;
    padding: 0;
}
.wraper{
    width: 100%;
    height: 500px;
}
.ls, .rs{
    position: relative;
    float: left;
    width: 30%;
    height: 100%;
    background-color: #eeeeee;
}
.c{
    position: relative;
    float: left;
    width: 40%;
    height: 100%;
    background-color: #dddddd;
}
.cdiv{
    position: absolute;
    top: 25%;
    left: 10%;
    width: 80%;
    height: 50%;
    background-color: red;
}

.ldiv{
    position: absolute;
    top: 30%;
    left: -80%;
    width: 80%;
    height: 40%;
    background-color: red;
}
.rdiv{
    position: absolute;
    top: 30%;
    right: -40%;
    width: 80%;
    height: 40%;
    background-color: red;
}

Javacsript标记:

//ASsign global variables
var mouseX = 0;
var newTop = 0;

$("div.ls").mousemove(function(event) {
    // Get parrent offset
    var parentOffset = $(this).offset();
    // Get child division offset
    var division = $(".ldiv").offset();
    // Calculate mouse position inside left division
    var relX = event.pageX - parentOffset.left;
    var relY = event.pageY - parentOffset.top;
    // Check if mouse changed its position
    if (mouseX != relX){
        // Get new position of x axis
        var newPosX = $(this).width() - relX - 161;
        // Get new height of child element in percentage
        var newHeight = 100 * parseFloat($(".ldiv").height()) / parseFloat($(this).height());
        // Display important stuff
        $(".coordinates").text("Mouse position = X:" + relX + " Y:" + relY + "Div offset = X:" + division.left + " Y:" + division.top  + " Width = " + $(this).width()+" newHJeight = "+newHeight);
        //If mouse moves left
        if (mouseX > relX) {
            // Cant go lower then 0.2 because javascript is rounding it down
            newHeight += 0.2;
            // calculate new top so division stays in middle of parent
            newTop = (100 - newHeight) / 2;
            // Assign new css
            $(".ldiv").css({
                left: newPosX + "px",
                height: newHeight + "%",
                top: newTop + "%"
            });
        } 
        //If mouse moves right
        else {
            newHeight -= 0.2;
            newTop = (100 - newHeight) / 2;
            $(".ldiv").css({
                left: newPosX + "px",
                height: newHeight + "%",
                top: newTop + "%"
            });
        }
        // Record mouse position
        mouseX = relX;
    }
});

​</p>

这是jsFiddle中的实时示例

我想完成的事情

  1. 我怎样才能重写这段代码,使它更像动画,并且如果我将鼠标移动得快就不会出现故障?

​</p>

4

1 回答 1

0

我不会依赖 mousemove,而是使用 requestAnimationFrame [或简单的 setTimeout]。这样你的 cpu 上的负载就会更小,动画也会更流畅。

于 2012-07-26T17:00:52.540 回答