jQuery 点击div, 向上展示内容

之前在网上看来的一个博主的demo, 但是当时没有记清, 现在默写一下...

原理:

  1. 首先红色div是通过position:absolute绝对定位的,且通过相对与底部定位,如bottom:0px。

  2. 底部定位固定,高度变高的时候就向上扩展了。

  3. 使用jQuery的$().animate()动画方法,控制红色div高度变化。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style type="text/css">
    .big{position:relative; width:200px; height:300px; background:#ccc}
    .show{position:absolute; display:none; bottom:0px;display:block;  width:100%; height:auto; background:#f66 }
</style>
<script type="text/javascript" src="jquery-1.8.3.min.js"</script>
<script type="text/javascript">
$(document).ready(function()
  {
    $(".big").hover(function () {
        $(".show").stop(true,true).animate({height:"100px"});
    }, function () {
        $(".show").stop(true,true).animate({height:"0px"});
    });
});
</script>
</head>
<body>
    <!--灰色的div-->
    <div class="big">
        <!--红色的div-->
        <div class="show"></div>
    </div>

</body>
</html>

其中, 如果是点击事件, 可以改为

$(".big").on('click', function () {
        $(".show").stop(true,true).animate({height:"70px"});
    });
posted @ 2017-09-18 21:06  DarthBadwolf  阅读(150)  评论(0编辑  收藏  举报