二十、手风琴效果

二十、手风琴效果

UVtmm6.png

  • 分析:

    • 我们将最外层容器宽定为500px,里面有5张图片,每一张平均分配的位置是100px。

    • 当鼠标移入后,当前图片展开,占220px,其他图片要压缩,每张平均分配70px。

20.1 DOM

1
2
3
4
5
6
7
8
9
<div class="carousel" id="carousel">
    <ul class="unit" id="unit">
        <li class="no0" id="no0"><img src="images/lunbo/1.jpg" alt=""></li>
        <li class="no1" id="no1"><img src="images/lunbo/2.jpg" alt=""></li>
        <li class="no2" id="no2"><img src="images/lunbo/3.jpg" alt=""></li>
        <li class="no3" id="no3"><img src="images/lunbo/4.jpg" alt=""></li>
        <li class="no4" id="no4"><img src="images/lunbo/5.jpg" alt=""></li>
</ul>
</div>

20.2 css

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* 清楚默认样式 */
* {
    margin0;
    padding0;
}
ulol {
    list-style: none;
}
a {
    text-decoration: none;
}
.carousel {
    width500px;
    height150px;
    margin50px auto;
    border3px solid #000;
    position: relative;
    overflow: hidden;
}
.carousel ul li {
    position: absolute;
    width220px;
    height150px;
    top0;
    left0;
}
.carousel ul li.no1 {
    left100px;
}
.carousel ul li.no2 {
    left200px;
}
.carousel ul li.no3 {
    left300px;
}
.carousel ul li.no4 {
    left400px;
}

20.3 jquery

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<!-- 引入jquery包 -->
<script src="js/jquery-3.4.1.min.js"></script>
<script>
    // 获取元素 命名时加上$提醒我们是一个jquery对象
    var $no0 = $("#no0");
    var $no1 = $("#no1");
    var $no2 = $("#no2");
    var $no3 = $("#no3");
    var $no4 = $("#no4");
    var $carousel = $("#carousel");

    // 添加鼠标移入事件
    $no0.mouseenter(function(){
        // 防流氓
        $("li").stop(true);
        $no1.animate({"left"220},500);
        $no2.animate({"left"220 + 70},500);
        $no3.animate({"left"220 + 70 * 2},500);
        $no4.animate({"left"220 + 70 * 3},500);
    });
    $no1.mouseenter(function(){
        // 防流氓
        $("li").stop(true);
        $no1.animate({"left"70},500);
        $no2.animate({"left"220 + 70},500);
        $no3.animate({"left"220 + 70 * 2},500);
        $no4.animate({"left"220 + 70 * 3},500);
    });
    $no2.mouseenter(function(){
        // 防流氓
        $("li").stop(true);
        $no1.animate({"left"70},500);
        $no2.animate({"left"70 * 2},500);
        $no3.animate({"left"220 + 70 * 2},500);
        $no4.animate({"left"220 + 70 * 3},500);
    });
    $no3.mouseenter(function(){
        // 防流氓
        $("li").stop(true);
        $no1.animate({"left"70},500);
        $no2.animate({"left"70 * 2},500);
        $no3.animate({"left"70 * 3},500);
        $no4.animate({"left"220 + 70 * 3},500);
    });
    $no4.mouseenter(function(){
        // 防流氓
        $("li").stop(true);
        $no1.animate({"left"70},500);
        $no2.animate({"left"70 * 2},500);
        $no3.animate({"left"70 * 3},500);
        $no4.animate({"left"70 * 4},500);
    });
    // 给carousel添加鼠标离开事件
    $carousel.mouseleave(function(){
        // 防流氓
        $("li").stop(true);
        $no1.animate({"left"100},500);
        $no2.animate({"left"100 * 2},500);
        $no3.animate({"left"100 * 3},500);
        $no4.animate({"left"100 * 4},500);
    })
</script>
点击查看

本文标题:二十、手风琴效果

文章作者:Mango

发布时间:2020年07月08日 - 22:24:01

最后更新:2020年07月08日 - 22:35:39

原始链接:https://mango185.github.io/post/59c74fea.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------------本文结束 感谢您的阅读-------------------