jquery - 使用 jQuery 展开两个 div 元素

标签 jquery html css

我有两个 div 元素,每个元素的宽度为 50%。我想在悬停时将一个 div div 扩大到 70%,并将另一个 div 缩小到 30%。

然后,当鼠标移开时,两者都恢复到 50%。我尝试了附加的代码,但没有用。

请问我怎样才能让它工作?

<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery Flex Hover Slider Demo</title>
    <style>

        #wrapper {
    width:100%;
    overflow:hidden;
    white-space:nowrap;
}
#left, #right {
    display:inline-block;
    width: 50%;
}
#left {
    background:red;
}
#right {
    background:yellow;
}

    </style>
    
    
    <script>
    
        $("#left, #right").each(function() {
        $(this).data("standardWidth", $(this).width());
    });

    $("#left, #right").hover(function() {
        $(this).animate({
            width: "70%"
        }, 300 );
        $(this).parent().children().not(this).animate({
            width: "30%"
        }, 300 );
    }, function() {
        $(this).parent().children().each(function() {
            $(this).animate({
                width: $(this).data("standardWidth")
            }, 300 );
        });
    });
    
    </script>
</head>

<body>
    <div id="wrapper">
        <div id="left" class="content_left">LEFT</div>
        <div id="right" class="content_right">RIGHT</div>
    </div>

    
</body>
</html>

最佳答案

更改了我的答案

你忘了把 jQuery 库代码放在 head 部分,你的 jQuery 脚本应该在文档的末尾

<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <title>jQuery Flex Hover Slider Demo</title>
    <style>
        #wrapper {
            width: 100%;
            overflow: hidden;
            white-space: nowrap;
        }
        #left,
        #right {
            display: inline-block;
            width: 50%;
        }
        #left {
            background: red;
        }
        #right {
            background: yellow;
        }
    </style>
</head>

<body>
    <div id="wrapper">
        <div id="left" class="content_left">LEFT</div>
        <div id="right" class="content_right">RIGHT</div>
    </div>

    <script>
        $('#left, #right').each(function() {
            $(this).data('standardWidth', $(this).width());
        });

        $('#left, #right').hover(
            function() {
                $(this).animate(
                    {
                        width: '70%'
                    },
                    300
                );
                $(this)
                    .parent()
                    .children()
                    .not(this)
                    .animate(
                        {
                            width: '30%'
                        },
                        300
                    );
            },
            function() {
                $(this)
                    .parent()
                    .children()
                    .each(function() {
                        $(this).animate(
                            {
                                width: $(this).data('standardWidth')
                            },
                            300
                        );
                    });
            }
        );
    </script>
</body>

关于jquery - 使用 jQuery 展开两个 div 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53355443/

相关文章:

javascript - 在文本更改时运行 javascript

javascript - contenteditable <div> 中插入符号位置处的 HTML 节点

javascript - 显示相对于 &lt;textarea&gt; 的 <div>

javascript - jQuery .click() 激活 addClass() & 类使用转换 : translate to move element

ipad - iPad 的媒体查询

jquery - Solr、Tomcat 和 CORS

html - React useEffect 保存文本区域文本

HTML/CSS : how to put all <li> in <ul> on the same line and centered

javascript - (重新安排)Codeigniter。如果它在侧边栏上,我应该在哪里/如何编写登录方法

PHP:浏览数据