html 缩小展开效果,jQuery实现DIV层的收缩展开效果

jQuery实现DIV层的收缩展开效果_网页代码站(www.webdm.cn)

/* 收缩展开效果 */

.text{line-height:22px;padding:0 6px;color:#666;}

.box h1{padding-left:10px;height:22px;line-height:22px;background:#f1f1f1;font-weight:bold;}

.box{position:relative;border:1px solid #e7e7e7;}

// 收缩展开效果

$(document).ready(function(){

$(".box h4").toggle(function(){

$(this).next(".text").animate({height: 'toggle', opacity: 'toggle'}, "slow");

},function(){

$(this).next(".text").animate({height: 'toggle', opacity: 'toggle'}, "slow");

});

});

收缩

1

2

3

4

5

6

7

8

9

收缩

1

2

3

4

5

6

7

8

9

可以使用jQuery的`slideToggle()`方法来实现同一页面多个DIV展开收缩功能。具体步骤如下: 1. 给需要展开收缩DIV添加一个class类,比如"expandable"。 2. 在jQuery中使用`$(".expandable")`来选择所有的需要展开收缩DIV。 3. 使用`click()`方法来监听DIV的点击事件。 4. 在点击事件中使用`slideToggle()`方法来展开收缩DIV。 示例代码如下: ```html <!DOCTYPE html> <html> <head> <title>HTML展开jQuery实现同一页面多个DIV展开收缩功能</title> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <style type="text/css"> .expandable { background-color: #f0f0f0; padding: 10px; margin-bottom: 10px; cursor: pointer; } .expandable-content { display: none; } </style> </head> <body> <div class="expandable"> <h3>第一</h3> <div class="expandable-content"> <p>第一内容</p> </div> </div> <div class="expandable"> <h3>第二</h3> <div class="expandable-content"> <p>第二内容</p> </div> </div> <div class="expandable"> <h3>第三</h3> <div class="expandable-content"> <p>第三内容</p> </div> </div> <script type="text/javascript"> $(document).ready(function() { $(".expandable").click(function() { $(this).find(".expandable-content").slideToggle(); }); }); </script> </body> </html> ``` 在上述代码中,我们使用了一个`.expandable`的class来选择需要展开收缩DIV,并且在点击事件中使用了`slideToggle()`方法来展开收缩DIV。同时,我们也添加了一些CSS样式来美化界面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值