综合案例(窗口拖动,css3线性运动动画,轮播图,跑马灯,闪动的红点)

案例一

盒子拖拽案例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>拖拽案例</title>
    <style>
    * {
        padding:0px;
        margin:0px
    }
    .login {
        width: 300px;
        height:300px;
        background: pink;
        position: absolute;
    }
    .login .header {cursor:move;width:100%;height:100px;background: black;}
    </style>
</head>
<body>
    <div class="login">
        <div class="header"></div>
    </div> 
</body>
<script>
    var headero=document.querySelector(".header")
        //1. header绑定按下事件
    headero.onmousedown=function(e){
       var olde=e||window.event
        //2. 事件处理函数中  给整个网页绑定移动事件
       document.onmousemove=function(e){
        var e=e||window.event
        //3. 移动事件处理函数中 1获取坐标 2给login设置
        var logino=document.querySelector(".login")
        logino.style.left=e.pageX-olde.offsetX+"px"  //用浏览器窗口左上角为原点的位置 减去 以header左上角为原点的位置来计算鼠标点击的位置
        logino.style.top=e.pageY-olde.offsetY+"px"  //同理
       }
            //n. 网页鼠标松开 取消移动
    headero.onmouseup=function(){
        document.onmousemove=null
    }
   }
</script>
</html>

在这里插入图片描述
注意点:
1、想要按住黑色区域内任意位置拖动整个方块,需要用
e.pageX-olde.offsetX+“px”
用浏览器窗口左上角为原点的位置 减去 以header左上角为原点的位置来计算鼠标点击的位置
2、鼠标松开便不再控制方块位置,要重新给header绑上onmouseout事件,然后在事件处理函数中将整个网页(document)的移动事件(onmousemove)移除
即:
headero.οnmοuseοut=function(){
document.οnmοusemοve=null
}

案例二

css3完成简单的线性运动动画效果(无限旋转,右移后恢复原位置再右移重复运动)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>css3实现线性运动动画</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #box{
            margin: 100px auto;
            width: 200px;
            height: 200px;
            background: red;
            background-size: cover;
        }
        .img{
            width: 200px;
            height: 200px;
        }
        #box span{
            width: 40px;
            height:60px;
            background-color: black;
            color: #fff;
            line-height: 60px;
            position: absolute;
            top: 50%;
            right: -40px;
            margin-top: -30px;
        }
        /* 使用动画类(标签上使用这个类名就会有动画效果)
        animateClass:自定义出来的动画类名
        1s:运动速度,
        linear:线性运动
        infinite:无限的重复运动(加了此属性就会重复运动,不加就只运动一次结束)
         */
        .dongAnin{
	        animation: animateClass 1s linear infinite;
            }
        /* 创建动画类 */
        @keyframes animateClass {
            from{  
                /* 向右移动 */
                /* transform: translateX(0); */
                /* 旋转 */
                transform:rotate(0)
            }
            to{
                /* 向右移动 */
                /* transform: translateX(50%); */
                /* 旋转 */
            transform:rotate(360deg)
            }
        }
    </style>
</head>
<body>
    <div id="box" class="dongAnin">
    </div>
</body>
<script >

</script>
</html>

在这里插入图片描述

案例三

可手动点击下一页上一页或自动播放的轮播图(原生js)

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图案例</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        li {
            list-style: none;
        }

        .big_box {
            position: relative;
            width: 500px;
            height: 400px;
            margin: 100px auto;
            overflow: hidden;
        }

        .big_box ul {
            position: absolute;
            left: 0;
            width: 600%;
            height: 400px;
        }

        .big_box ul li {
            float: left;
        }

        .big_box ul li img {
            width: 500px;
            height: 400px;
        }

        .left {
            position: absolute;
            top: 50%;
            left: 0;
            transform: translateY(-50%);
            background-color: rgba(255, 255, 255, .5);
            width: 30px;
            height: 30px;
            line-height: 30px;
            text-align: center;
        }

        .right {
            position: absolute;
            top: 50%;
            right: 0;
            transform: translateY(-50%);
            background-color: rgba(255, 255, 255, .5);
            width: 30px;
            height: 30px;
            line-height: 30px;
            text-align: center;
        }

        .big_box ol {
            position: absolute;
            bottom: 0;
            /* 这里别给ol限制宽度,让小圆圈的个数自动把ol撑大 */
            left: 50%;
            transform: translateX(-50%);
            height: 10%;
            line-height: 10%;
            text-align: center;
        }

        .big_box ol li {
            float: left;
            width: 20px;
            height: 20px;
            background-color: rgba(0, 0, 0, .5);
            border-radius: 50%;
            margin-left: 5px;
        }

        .big_box ol .current {
            background-color: rgb(211, 29, 29);
        }

        .left,
        .right,
        ol li {
            cursor: pointer;
        }
    </style>
</head>

<body>

    <div class="big_box">
        <!-- 轮播的图片 -->
        <ul>
            <li>
                <img src="https://img-blog.csdnimg.cn/43255527f430420bad42be39a6f0e94d.png" alt="">
            </li>
            <li>
                <img src="https://img-blog.csdnimg.cn/bff6932a9b224fa996b66ae6388590e6.png" alt="">
            </li>
            <li>
                <img src="https://img-blog.csdnimg.cn/ad4631d47b364797bae98bc6f2ea4852.png" alt="">
            </li>
        </ul>
        <!-- 左右按键 -->
        <div class="left">&lt</div>
        <div class="right">&gt</div>
        <!-- 小圆圈 -->
        <ol>
        </ol>
       
    </div>
    <script>
        var timer;
        var big_box = document.querySelector('.big_box');
        var ul = big_box.querySelector('ul');
        var ol = big_box.querySelector('ol');
        var lis_img = ul.querySelectorAll('li');
        var left = document.querySelector('.left');
        var right = document.querySelector('.right');
        var num = 0;  //记录要滑到第几张图片
        
        //封装运动函数
        function animate(obj, target) {
            var timer1 = setInterval(function () {
                var current = obj.offsetLeft;
                var step = 10;
                step = current > target ? -step : step;
                // 下面要包括等于的情况,否则会发生抖动
                if (Math.abs(current - target) <= Math.abs(step)) {
                    clearInterval(timer1);
                    obj.style.left = target + 'px';
                }
                else {
                    obj.style.left = current + step + 'px';
                }
            }, 10)
        }

        //小圆圈样式改变
        function circlechange(circles, circle) {
            if (circle == lis_img.length) {
                circle = 0;
            }
            //排他思想设置小圆圈样式
            //排他思想第一步:先把所有的小圆圈样式去掉
            for (var i = 0; i < circles.length; i++) {
                circles[i].className = "";
            }
            //排他思想第二步:把当前图片对应的小圆圈设置样式
            circles[circle].className = "current";
        }

        //在页面刚加载进来就执行代码
        window.addEventListener('load', function () {
            //设置小圆点的个数
            for (var i = 0; i < lis_img.length; i++) {
                var li = document.createElement('li');
                ol.appendChild(li);
                // 给小圆圈添加自定义属性
                li.setAttribute('index', i);
                //一开始第一个小圆圈就是被选中状态
                if (i == 0) {
                    li.className = "current";
                }
                //给小圆圈添加点击处理事件
                li.addEventListener('click', function () {
                    //排他思想实现小圆圈样式改变
                    for (var j = 0; j < ol.children.length; j++) {
                        ol.children[j].className = "";
                    }
                    this.className = "current";
                    //实现点击小圆圈后图片滑动
                    var index = this.getAttribute('index');
                    animate(ul, -index * big_box.offsetWidth);
                    // 在图片滑动的同时对应的小圆圈样式也要发生改变,所以调用animate函数同时调用circlechange函数
                    circlechange(circles, index);
                })
            }
            //为了实现无缝衔接的切换图片,要把第一张图片克隆到最后一张图片的附近
            var circles = ol.querySelectorAll('li');
            // cloneNode函数若括号里面是true,则是深拷贝,false则是浅拷贝
            var li_img = ul.children[0].cloneNode(true);
            ul.appendChild(li_img);

            //点击右箭头向右滑动
            right.addEventListener('click', function () {
                //下面if代码是实现向右滑动的无缝衔接,不懂的建议自己手动模拟一遍
                //点击num+1,当num大于或等于图片的数量时就归零,好让轮播回到第一张图
                if (num >= lis_img.length) {
                    num = 0;
                    //注意改变属性left的值后面一定要跟px,否则没有效果
                    ul.style.left = 0 + 'px';
                }
                num++;
                animate(ul, -num * big_box.offsetWidth);
                circlechange(circles, num);
            })
            //点击左箭头向左滑动
            left.addEventListener('click', function () {
                //下面if代码是实现向左滑动的无缝衔接,不懂的建议自己手动模拟一遍
                if (num <= 0) {
                    num = lis_img.length;
                    ul.style.left = -lis_img.length * big_box.offsetWidth + 'px';
                }
                num--;
                animate(ul, -num * big_box.offsetWidth);
                circlechange(circles, num);
            })

            //实现自动播放----因为自动播放的功能和向右滑动的功能一样,所以直接调用向右滑动的函数
            timer = setInterval(function () {
                right.click();
            }, 1000)

            //鼠标放到盒子上停止自动播放
            big_box.addEventListener('mouseover', function () {
                clearInterval(timer);
            })

            //鼠标离开自动播放
            big_box.addEventListener('mouseout', function () {
                clearInterval(timer);
                //在重新创建一个定时器时最好先清除一下定时器
                timer = setInterval(function () {
                    right.click();
                }, 1000)
            })
        })
        window.addEventListener('onunload', function (){
            console.log(99999);
            clearInterval(timer);
        })
    </script>
</body>

</html>

效果图
在这里插入图片描述

案例四

跑马灯案例(原生js,默认是js实现的效果,内部也有只用css动画实现的代码默认被注释着的)

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>向左匀速运动的走马灯效果</title>
    <style>
        .container {
            box-sizing: border-box;
            overflow: hidden;
            position: relative;
            width: 1000px;
            height: 200px;
            margin: 50px auto 0px;
            border: 1px solid red;
        }
 
        .container .wrapper {
            position: absolute;
            top: 0px;
            left: 0px;
            box-sizing: border-box;
            width: 2200px;
            height: 200px;
        }
 
        .container .wrapper .slider {
            box-sizing: border-box;
            float: left;
            width: 200px;
            height: 200px;
            padding: 10px 20px;
        }
 
        .container .wrapper .slider span {
            display: block;
            /* 不换行三件套 */
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        .container .wrapper .slider img {
            width: 150px;
            height: 150px;
        }
        /* 这跑马灯的效果只用css动画也可实现,以下注释的css动画样式就是用css动画实现的代码 */
        /* .container .wrapper  {
            animation: test 5s infinite linear;
        }
        @keyframes test {
            from  {
                left: 0px;
            }
            to {
                left: -1200px;
            }
        } */
        .container:hover .wrapper {
            animation-play-state: paused;
        }
       
 
    </style>
</head>
 
<body>
    <div class="container ">
        <div class="wrapper ">
            <!-- 每一张图片slider -->
            <div class="slider">
                <img src="./img/1.jpg">
                <span>天地壹号 清爽酸甜 苹果醋22222222222</span>
            </div>
            <div class="slider">
                <span>天地壹号 清爽酸甜 苹果醋22222222222</span>
                <img src="./img/2.jpg">
            </div>
            <div class="slider">
                <img src="./img/3.jpg">
                <span>天地壹号 清爽酸甜 苹果醋</span>
            </div>
            <div class="slider">
                <span>天地壹号 清爽酸甜 苹果醋</span>
                <img src="./img/4.jpg">
            </div>
            <!-- 无缝的 -->
            <div class="slider">
                <span>天地壹号 清爽酸甜 无缝的苹果醋</span>
                <img src="./img/1.jpg">
            </div>
            <div class="slider">
                <img src="./img/2.jpg">
                <span>天地壹号 清爽酸甜 无缝的苹果醋</span>
            </div>
            <div class="slider">
                <img src="./img/3.jpg">
                <span>天地壹号 清爽酸甜 苹果醋</span>
            </div>
            <div class="slider">
                <span>天地壹号 清爽酸甜 苹果醋</span>
                <img src="./img/4.jpg">
            </div>
        </div>
    </div>
</body>
 <script>
            //获取装照片的盒子
         var wrapper = document.querySelector('.wrapper'),
            timer = null,
            step = -20,
            left = 0;
            //页面打开就开始加载动画
        window.onload = function () {
            timer = setInterval(autoMove, 100)
        }
        //鼠标移入清除定时器,停止运动
        wrapper.addEventListener('mouseenter',function(){
            clearInterval(timer)
        })
           //鼠标移出继续运动
        wrapper.addEventListener('mouseleave',function(){
            timer = setInterval(autoMove, 100)
        })
        //封装运动函数
        function autoMove() {
            //获取wrapper盒子往左边运动的距离
            left = getCss(wrapper, 'left');
            console.log(left);
            left += step   //100毫秒加-20(即每过100毫秒就往左运动10px)
            if (left < -1200) {//当往左运动超过1200px时就将wrapper距离左边距离归零,过渡清零
                wrapper.style.left = 0 + 'px';
                wrapper.style.transition = 'all 0s'
                left = getCss(wrapper, 'left');
            }
            wrapper.style.left = left + 'px';//往左运动不超过1200px就继续加
            wrapper.style.transition = 'all .3s' //并添加0.3s的过渡
        }
        //封装获取元素css样式的方法
        //getComputedStyle只能读取样式,不可以修改样式,会引起回流。
        function getCss(culEle, attr) {
            return parseFloat(window.getComputedStyle(culEle, null)[attr]);
        }
 </script>
</html>

效果图

在这里插入图片描述

案例五

css动画闪动的红点

<html>

<head>
  <meta charset="utf-8">
  <meta data-n-head="1" name="viewport" content="width=device-width, initial-scale=1">
  <style type="text/css">
    body {
      display: flex;
      align-items: center;
      justify-content: center;
      min-height: 100vh;
      background-color: #1f3244;
    }
    .container {
      width: 400px;
      height: 400px;
    }

    .pulse {
      background-color: coral;
      height: 200px;
      width: 200px;
      border-radius: 100%;
      position: relative;
    }

    .ring {
      position: absolute;
      background-color: inherit;
      height: 100%;
      width: 100%;
      border-radius: 100%;
      opacity: 0.8;
      animation: pulsing 2s ease-out infinite;
    }

    .ring:nth-of-type(1) {
      animation-delay: -0.5s;
    }

    .ring:nth-of-type(2) {
      animation-delay: -1s;
    }

    .ring:nth-of-type(3) {
      animation-delay: -1.5s;
    }


    @keyframes pulsing {
      100% {
        transform: scale(1.75);
        opacity: 0;
      }
    }
  </style>
</head>

<body>
  <div class="container">
    <div class="pulse">
      <div class="ring"></div>
      <div class="ring"></div>
      <div class="ring"></div>
      <div class="ring"></div>
    </div>

  </div>

</body>

</html>

效果图
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现这样的效果,需要使用Vue.js和一些CSS和JavaScript技巧。以下是一种实现方式: 1. 首先,创建一个Vue组件,包含一个3D轮和一些CSS样式,用于让轮旋转和显示。 ``` <template> <div class="carousel-container"> <div class="carousel"> <div class="slide" v-for="(item, index) in items" :key="index"> <img :src="item.image" alt="slide" class="slide-image"> <div class="slide-content"> <h3 class="slide-title">{{ item.title }}</h3> <p class="slide-text">{{ item.text }}</p> </div> </div> </div> </div> </template> <script> export default { data() { return { items: [ { image: "https://picsum.photos/400/300", title: "Slide 1", text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit." }, { image: "https://picsum.photos/400/300", title: "Slide 2", text: "Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." }, { image: "https://picsum.photos/400/300", title: "Slide 3", text: "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat." } ] }; } }; </script> <style scoped> .carousel-container { position: relative; width: 100%; height: 400px; overflow: hidden; } .carousel { position: absolute; top: 0; left: 0; right: 0; bottom: 0; margin: auto; width: 80%; height: 80%; transform-style: preserve-3d; transform: translateZ(-1000px); animation: rotate 15s linear infinite; } .slide { position: absolute; top: 0; left: 0; right: 0; bottom: 0; margin: auto; width: 400px; height: 300px; border-radius: 10px; box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.5); transform-style: preserve-3d; backface-visibility: hidden; } .slide-image { width: 100%; height: 100%; object-fit: cover; border-radius: 10px; } .slide-content { position: absolute; bottom: 0; left: 0; right: 0; padding: 20px; background-color: rgba(0, 0, 0, 0.5); color: #fff; text-align: center; } .slide-title { font-size: 32px; margin-bottom: 10px; } .slide-text { font-size: 18px; } </style> ``` 2. 使用CSS和JavaScript添加拖拽飞屏效果。在组件的mounted生命周期钩子中,添加以下代码: ``` mounted() { let startX, startY, currentX, currentY, distanceX, distanceY, isDragging = false; const carousel = this.$el.querySelector(".carousel"); carousel.addEventListener("mousedown", e => { startX = e.clientX; startY = e.clientY; isDragging = true; carousel.style.animation = "none"; }); carousel.addEventListener("mousemove", e => { if (!isDragging) return; e.preventDefault(); currentX = e.clientX; currentY = e.clientY; distanceX = currentX - startX; distanceY = currentY - startY; carousel.style.transform = `translateX(${distanceX}px) translateY(${distanceY}px) translateZ(-1000px) rotateY(${distanceX / 10}deg) rotateX(${distanceY / 10}deg)`; }); carousel.addEventListener("mouseup", e => { isDragging = false; carousel.style.animation = "rotate 15s linear infinite"; carousel.style.transform = `translateZ(-1000px)`; }); carousel.addEventListener("mouseleave", e => { isDragging = false; carousel.style.animation = "rotate 15s linear infinite"; carousel.style.transform = `translateZ(-1000px)`; }); } ``` 这段代码监听轮的mousedown、mousemove、mouseup和mouseleave事件,在鼠标按下时记录起始坐标,移动时根据坐标计算移动距离,并将轮的transform样式设置为相应的位移和旋转角度。在鼠标抬起或离开轮时,将轮的transform样式重置,并重新启动旋转动画。 这样就实现了一个Vue 3D轮上下拖拽飞屏效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值