• 技术文章 >Web开发 >css教程

    CSS实现轮播图的方法

    小妮浅浅小妮浅浅2021-08-30 09:47:40转载7181

    实现思路

    1、通过animation达到动起来的效果,具体变化似乎有两种可行方式:

    2、在动画中,通过CSS-transform不断平移轮播图元素位置。

    3、在动画中,设置不同的left值。

    实现效果与代码

    其中值得注意的点在于需要手动在轮播图头部添加最后一张图的复制,否则会有明显的闪动效果。

    实例

    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

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    76

    77

    78

    79

    80

    81

    82

    83

    84

    85

    86

    87

    88

    89

    90

    91

    92

    93

    94

    95

    96

    97

    98

    99

    100

    101

    102

    103

    104

    105

    106

    107

    108

    109

    110

    111

    112

    113

    114

    115

    116

    117

    118

    119

    120

    121

    122

    123

    124

    125

    126

    127

    128

    129

    130

    131

    132

    133

    134

    135

    136

    <!DOCTYPE html>

      

    <body>

        <div style="flex: 1;height: 300px;z-index: 10;box-shadow: inset 0 0 300px rgba(0, 0, 0, 0.99);">

            left

        </div>

        <div class="showbox border box-shadow">

            <div class="left border">

                

            </div>

            <div class="right border">

                

            </div>

            <div id="imgbox" class="center imgbox">

                <img src="https://cdn.pixabay.com/photo/2018/01/03/05/33/the-sun-3057622__340.jpg" />

                <img src="https://cdn.pixabay.com/photo/2021/07/29/20/23/mountains-6508015_960_720.jpg" />

                <img src="https://cdn.pixabay.com/photo/2021/07/29/21/03/cereals-6508088__340.jpg" />

                <img src="https://cdn.pixabay.com/photo/2018/01/03/05/33/the-sun-3057622__340.jpg" />

            </div>

        </div>

        <div  style="flex: 1;height: 300px;z-index: 10;box-shadow: inset 0 0 300px rgba(0, 0, 0, 0.99);" >

            right

        </div>

    </body>

    <!-- <script>

        let a = 0

        let max = 300 * 3;

        window.onload = function() {

            refresh();

        }

      

        function refresh() {

            document.getElementById("imgbox").style.left = a + "px";

        }

      

        function left() {

            a = (a-300)%max;

            refresh();

        }

      

        function right () {

            a = (a+300)%max;

            refresh();

        }

      

    </script> -->

    <style>

        body {

            width: 100%;

            height: 100%;

            z-index: 0;

            /* background-color: rgba(0, 0, 0, 0.5); */

            box-shadow: inset 0 0 300px rgba(0, 0, 0, 0.1);

        }

      

        .center {

            display: flex;

            flex-direction: row;

            align-items: center;

            justify-content: center;

        }

      

        .showbox {

            width: 300px;

            height: 300px;

            /* background: chocolate; */

            position: relative;

            overflow: visible;

      

            display: flex;

            flex-direction: row;

            align-items: center;

            justify-content: center;

            /* z-index: -1; */

            opacity: 1;

        }

      

        .left {

            position: absolute;

            left: 0;

            top: 50%;

            cursor: pointer;

            background: blue;

            z-index: 100;

        }

      

        .right {

            position: absolute;

            right: 0;

            top: 50%;

            cursor: pointer;

            background: blue;

            z-index: 100;

        }

      

        .border {

            border: 1px solid black;

        }

      

        .centerimg {

            width: 100%;

            height: 100%;

        }

      

        .myimg {

            width: 300px;

            height: 300px;

            z-index: -1;

            opacity: 1;

            /* filter: alpha(opacity=60); */

        }

        .imgbox {

            position: absolute;

            left: -600px;

            top: 0;

            z-index: -1;

            animation: slideshow 10s both infinite;

        }

      

        @keyframes slideshow {

            0% {

                left: -900px;

            }

            33% {

                left: -600px;

            }

            66% {

                left: -300px;

            }

            100% {

                left: 0;

            }

        }

    </style>

      

    </html>

    以上就是CSS实现轮播图的方法,希望对大家有所帮助。更多css学习指路:css教程

    本文教程操作环境:windows7系统、css3版,DELL G3电脑。

    专题推荐:css轮播图
    上一篇:css中outline方法的使用 下一篇:css内联样式的盒子模型

    相关文章推荐

    • css渐进增强和优雅降级的区别• css渐进增强如何理解• css优雅降级是什么意思• css margin:auto的填充规则• css中box-shadow方法如何使用• css中outline方法的使用• Vue框架在PostCSS中使用sass的方法

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网