javascript - JQuery id 测试失败,带有单独 div 的动画系统

标签 javascript jquery html css

我正在尝试制作一种新的菜单,在屏幕中间有 4 个方 block ,当您将鼠标悬停在这些方 block 上时,它们会放大,每个方向不同,因此它们不会重叠。我已经把动画部分和基本部分搞定了,唯一的问题是,我如何让它们朝不同的方向移动?我已经通过添加一个 if 语句来解决这个问题,该语句获取元素的 id 并使用它来找出正方形所在的位置。但是,这个声明失败了,我认为是在它检查 id 的部分。我可以借助一些帮助来解决这个问题,因为我基本上已经尝试了所有方法来让它工作。提前致谢!

这是带有工作动画的 JSFiddle:https://jsfiddle.net/2qkv1xua/ 这是相同的 JSFiddle,但添加了 if 语句:https://jsfiddle.net/2qkv1xua/1/

当然还有代码:

[HTML]

<body>
    <div id="menu_holder">
        <div class="menuItem" id="menu_item_1"></div>
        <div class="menuItem" id="menu_item_2"></div>
        <div class="menuItem" id="menu_item_3"></div>
        <div class="menuItem" id="menu_item_4"></div>
    </div>
</body>

[CSS]

* {
    margin: 0 auto;
    padding: 0;
}
#menu_holder {
    position: absolute;
    width: 410px;
    height: 410px;
    margin: 0;
    padding: 105px;
    top: calc(50% - 305px);
    left: calc(50% - 305px);
}
.menuItem {
    width: 200px;
    height: 200px;
    float: left;
}
#menu_item_1 {
    margin: 0px 5px 5px 0px;
    background-color: rgb(190, 184, 235);
}
#menu_item_2 {
    margin: 0px 0px 5px 5px;
    background-color: rgb(139, 157, 195);
}
#menu_item_3 {
    margin: 5px 5px 0px 0px;
    background-color: rgb(82, 153, 211);
}
#menu_item_4 {
    margin: 5px 0px 0px 5px;
    background-color: rgb(11, 85, 99);
}

[JS]

$(document).ready(function () {
    $('.menuItem').hover(
        if (this.attr('id') == "#menu_item_1") {
            function () {
                $(this).filter(':not(:animated)').animate({
                    width: "300px",
                    height: "300px",
                    marginTop: "-100px",
                    marginLeft: "-100px"
                });
            },
            function () {
                $(this).animate({
                    width: "200px",
                    height: "200px",
                    marginTop: "0",
                    marginLeft: "0"
                });
            }
        });
});

最佳答案

在每个 menu_item 周围都有包装器并且所有东西都绝对定位,你可以这样做:

<div id="menu_holder">
    <div class="menuWrapper" id="menu_wrapper_1"><div class="menuItem" id="menu_item_1"></div></div>
    <div class="menuWrapper" id="menu_wrapper_2"><div class="menuItem" id="menu_item_2"></div></div>
    <div class="menuWrapper" id="menu_wrapper_3"><div class="menuItem" id="menu_item_3"></div></div>
    <div class="menuWrapper" id="menu_wrapper_4"><div class="menuItem" id="menu_item_4"></div></div>
</div>
* {
    margin: 0 auto;
    padding: 0;
}
#menu_holder {
    position: absolute;
    width: 180px;
    height: 180px;
    left: 10px;
    top: 10px;
    border: 1px dashed #999;
}
.menuWrapper {
    position: absolute;
    width: 87px;
    height: 87px;
}
#menu_wrapper_1 {
    left: 0;
    top: 0;
}
#menu_wrapper_2 {
    right: 0;
    top: 0;
}
#menu_wrapper_3 {
    left: 0;
    bottom: 0;
}
#menu_wrapper_4 {
    right: 0;
    bottom: 0;
}
.menuItem {
    position: absolute;
    width: 50px;
    height: 50px;
}
#menu_item_1 {
    background-color: rgb(190, 184, 235);
    right: 0;
    bottom: 0;
}
#menu_item_2 {
    background-color: rgb(139, 157, 195);
    left: 0;
    bottom: 0;
}
#menu_item_3 {
    background-color: rgb(82, 153, 211);
    right: 0;
    top: 0;
}
#menu_item_4 {
    background-color: rgb(11, 85, 99);
    left: 0;
    top: 0;
}
$(document).ready(function () {
    $('.menuItem').hover(function () {
        $(this).stop().animate({
            width: "80px",
            height: "80px"
        });
    }, function () {
        $(this).stop().animate({
            width: "50px",
            height: "50px"
        });
    });
});

我减小了尺寸以使 fiddle 框架中的演示效果更好

DEMO

关于javascript - JQuery id 测试失败,带有单独 div 的动画系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31357451/

相关文章:

javascript - ng-model 不更新值

单击后退按钮时 Javascript 重定向

javascript - 如何从表单中获取 HTML(带有用户插入的数据)

jquery - 如何将 async/await 与 jquery ajax 一起使用?

html - CSS 悬停属性

javascript - jQuery仅隐藏单击的特定表行onclick事件

JavaScript 小数计算

jQuery .submit() 不触发

html - 如何访问 DOM 中 HTML 链接对象的颜色属性

javascript - Angular/Firebase - 从 Firebase 返回键和值