图片轮播

  思路:1.点击左右按钮移动图片,图片宽度一个视口宽度,

             2.让图片做到无限循环,即在跳转到缓冲图是将其定位在其原图的位置

             3.实现滑动,指定每次滑动距离,setinterva,在达到目的时clearinterval;

4.实现小按钮点击时与图片的位置挂钩;




  <!-- banner -->

    <div class="banner-wrap">
        <div id="content">                         //图片轮播时取两端的图片交换放在两端作为缓冲图
            <img src="imgs/banner2.jpg">
            <img src="imgs/banner.jpg">
            <img src="imgs/banner1.jpg">
            <img src="imgs/banner2.jpg">
            <img src="imgs/banner.jpg">
        </div>                             
        <div class="point">
            <a href="javascript:" style="background: red;"></a>
            <a href="javascript:"></a>
            <a href="javascript:"></a>
        </div>
        <div class="icons">
            <i class="back" id="back"></i>
            <i class="go" id="go"></i>
        </div>

    </div>

.banner-wrap {
        overflow: hidden;
        position: relative;
    }
    #content {
        left: -100%;     //由于图片是撑满整个屏的,这里用100%做单位,有利于缩放;
        position: relative;

        font-size: 0;
        white-space: nowrap;
    }
.banner-wrap .point {
    position: absolute;
    width: 100px;
    height: 20px;
    bottom: 20px;
    right: 50px;
}


.banner-wrap .point a {
    display: inline-block;
    height: 10px;
    width: 10px;
    margin: 0 10px;
    border-radius: 50%;
    background: #000;
}


.banner-wrap .back {
    display: inline-block;
    height: 30px;
    width: 30px;
    background: url(../imgs/back.png);
    background-size: 100% 100%;
}


.banner-wrap .go{
    display: inline-block;
    height: 30px;
    width: 30px;
    background: url(../imgs/go.png);
    background-size: 100% 100%;
    right: 0;
}
.banner-wrap .icons i{
    position: absolute;
    bottom: 50%;
}


    $(".banner-wrap i").click(function() {                                       //左右箭头点击时
        if ($("#content").attr("data-state") === "doing") return;       //判断是否在跳转中,如果在跳转中直接return掉
        var _id = $(this).attr("id");                       
        move(_id);
        redpoint(_id);
    });


    point.click(function() {
        var screenW = $(window).width(),
            currentLeft = parseFloat($("#content").css("left")) / screenW,
            imageindex = -1 * currentLeft - 1,
            _index = $(this).index();


        point.siblings().css("background", "#000");
        $(this).css("background", "red");


        if (_index > imageindex) {
            goal(-1);
        }
        if (_index < imageindex) {
            goal(1);
        }


        function goal(val) {
            var _times = 0;
            var change = setInterval(function() {
                var _currentLeft = parseFloat($("#content").css("left")) / screenW,   //content的left值/视口宽度,始终是一个负数
                    imageindex = -1 * currentLeft - 1,         //即当前显示的image的index
                    step = 0.1;                                            //设置每次移动距离
                $("#content").css("left", (_currentLeft + val * step) * 100 + "%"); //重新赋值给left
                _times += 1;
                // if (_times = Math.abs(_index-imageindex)*10) {
                //     clearInterval(change);
                // }
            }, 100);
        }
    });


    function redpoint(_id) {
        var screenW = $(window).width(),
            currentLeft = parseFloat($("#content").css("left")) / screenW,
            index = -1 * currentLeft - 1;
        //判断运动方向并且执行
        if (_id == "go") {
            index += 1;
            if (index > 2) {   //判断图片位置,跳转到原图
                index = 0;
            }


            point.siblings().css("background", "#000");
            point.eq(index).css("background", "red");
        } else {
            index -= 1;
            if (index < 0) {
                index = 2;
            }
            point.siblings().css("background", "#000");
            point.eq(index).css("background", "red");
        }
    }


    function move(_id) {
        var times = 0,
            screenW = $(window).width();
        $("#content").attr("data-state", "doing"); //打上标签
        var roll = setInterval(function() {
            currentLeft = parseFloat($("#content").css("left")) / screenW;


            //判断运动方向并且执行
            if (_id == "go") {
                $("#content").css("left", (currentLeft - 0.1) * 100 + "%");
            } else {
                $("#content").css("left", (currentLeft + 0.1) * 100 + "%");
            }


            //清除计时器
            times += 1;
            if (times === 10) {
                clearInterval(roll);
                $("#content").removeAttr("data-state");
                // 判断无限循环
                newCurrentLeft = Math.round(parseFloat($("#content").css("left")) / screenW);
                if (newCurrentLeft > -1) {
                    $("#content").css("left", "-300%");
                } else if (newCurrentLeft < -3) {
                    $("#content").css("left", "-100%")
                }
            }


        }, 100);

    }






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


<head>
    <meta charset="UTF-8">
    <title>图片轮播</title>
    <link rel="stylesheet" type="text/css" href="css/simple-cube.min.css">
    <style type="text/css">
    .nav-wrap {
        position: relative;
        overflow: hidden;
    }
    
    #content {
        white-space: nowrap;
        font-size: 0;
        position: relative;
        left: -100%;
    }
    
    .point {
        position: absolute;
        bottom: 20px;
        right: 20px;
    }
    
    .point a {
        display: inline-block;
        height: 10px;
        width: 10px;
        background: #000;
        margin: 0 10px;
        border-radius: 50%;
    }
    
    #on {
        background: red;
    }
    
    .nav-wrap i {
        display: inline-block;
        position: absolute;
        bottom: 50%;
    }
    
    #go {
        width: 40px;
        height: 40px;
        background: url(imgs/right.png);
        background-size: 100% 100%;
        right: 0;
    }
    
    #back {
        width: 40px;
        height: 40px;
        background: url(imgs/left.png);
        background-size: 100% 100%;
    }
    </style>
</head>


<body>
    <div class="nav-wrap">
        <div id="content">
            <img src="imgs/banner2.jpg">
            <img src="imgs/banner.jpg">
            <img src="imgs/banner1.jpg">
            <img src="imgs/banner2.jpg">
            <img src="imgs/banner.jpg">
        </div>
        <div class="point">
            <a href="javascript:"></a>
            <a href="javascript:"></a>
            <a href="javascript:"></a>
        </div>
        <i id="back"></i>
        <i id="go"></i>
    </div>
    <script type="text/javascript" src="js/jQuery2.2.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
        $(".point a").eq(0).css("background", "red");
        $("i").click(function() {
                if ($("#go").attr("type") == "doing") return;
                var _id = $(this).attr("id");
                move(_id);
                change();
                btnchange(_id);
            })
            // 图片移动
        function move(_id) {
            var time = 0
            $("#go").attr("type", "doing");
            var a = setInterval(function() {
                var screenW = $(window).width(),
                    currentLeft = parseFloat($("#content").css("left")) / screenW,
                    step = 0.1;
                if (_id == "go") {
                    $("#content").css("left", (currentLeft - step) * 100 + "%");
                } else {
                    $("#content").css("left", (currentLeft + step) * 100 + "%");
                }
                time += 1;
                if (time == 10) {
                    clearInterval(a);
                    $("#go").removeAttr("type");
                }
            }, 100);
        }
        // 图片轮播
        function change() {
            var screenW = $(window).width(),
                currentLeft = Math.round(parseFloat($("#content").css("left")) / screenW);
            if (currentLeft > -1) {
                $("#content").css("left", "-300%");


            } else if (currentLeft < -3) {
                $("#content").css("left", "-100%");


            }
        }


        // 按钮变换
        $(".point a").click(function() {
            var index = $(this).index();
            var screenW = $(window).width(),
                currentLeft = parseFloat($("#content").css("left")) / screenW,
                imageindex = -currentLeft - 1;
            $(this).css("background", "red");
            $(this).siblings().css("background", "#000");
            $("#content").css("left", (currentLeft - (index - imageindex)) * 100 + "%");


        })


        function btnchange(_id) {
        var screenW = $(window).width(),
                    currentLeft = Math.round(parseFloat($("#content").css("left"))) / screenW,
                    imageindex = -currentLeft - 1;
            if (_id == "go") {
                imageindex += 1;
                if (imageindex > 2 ) {
                imageindex = 0;
                }
                 $(".point a").eq(imageindex).css("background","red");
                 $(".point a").eq(imageindex).siblings().css("background","#000");
            }
        else if(_id == "back"){
        imageindex -= 1;
        if (imageindex < 0 ) {
                imageindex = 2;
                }
                $(".point a").eq(imageindex).css("background","red");
                $(".point a").eq(imageindex).siblings().css("background","#000");
        }
}


    })
    </script>
</body>


</html>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF(Windows Presentation Foundation)是一种用于创建Windows桌面应用程序的技术框架,可以实现丰富的用户界面和交互效果。图片轮播切换动画是在WPF中常见的一个功能,可以使图片在切换时产生平滑的过渡效果。 要实现WPF图片轮播切换动画,可以按照以下步骤进行: 1. 创建一个WPF窗体或用户控件,用于显示图片和进行动画效果的切换。 2. 在界面中添加一个Image控件,用于显示图片。可以通过设置Image的Source属性来加载要显示的图片。 3. 创建一个Storyboard对象,用于定义轮播切换的动画效果。可以使用WPF内置的动画效果,如Fade(淡入淡出)、Slide(滑动)等。根据需求调整动画的速度、延迟等属性。 4. 将动画效果添加到Storyboard中,并关联到Image控件的相关属性。例如,通过使用DoubleAnimation anim1 = new DoubleAnimation(0, 1, TimeSpan.FromSeconds(1))来定义一个从0到1的透明度变化动画效果,然后将其关联到Image的Opacity属性。 5. 创建一个Timer(计时器)对象,用于定时触发图片切换的动画效果。可以使用DispatcherTimer类来实现,设置时间间隔和Tick事件。 6. 在Timer的Tick事件中,根据需要切换要显示的图片。可以通过修改Image的Source属性来加载不同的图片。 7. 在Timer的Tick事件中,启动Storyboard开始动画效果的播放。可以调用Storyboard的Begin方法来启动。 8. 重复步骤6和7,实现图片轮播的切换效果。可以根据实际需求设定切换的频率和图片的数量。 通过以上步骤,就可以实现WPF图片轮播切换动画效果。可以根据实际需求调整动画效果的参数,如切换速度、图片大小、切换方式等,使得界面显示出更加美观和吸引人的效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值