【问题标题】:Animate css depending on mouse movement [how can I improve]根据鼠标移动动画 css [如何改进]
【发布时间】:2012-07-26 16:53:17
【问题描述】:

你好

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

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

代码

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;
    }
});

这是jsFiddle 中的实时示例

我想要完成的事情

  1. 如何重写此代码,使其更像动画,并且如果我将鼠标移动得快也不会出现故障?

【问题讨论】:

    标签: javascript jquery html css mousemove


    【解决方案1】:

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

    【讨论】:

    • 我知道你有什么想法,但我不知道你在说什么?
    • 让它尽可能简单:使用 mousemove 函数将全局变量 [或对象属性] 设置为所需的位置;与此同时,您将拥有另一个负责移动对象的“线程”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-15
    相关资源
    最近更新 更多