图片轮播

 页面代码

<div class="banner">
    <div class="carousel-content">
        <ul class="carousel">
            <li><img src="images/html_1.jpg"></li>
            <li><img src="images/html_2.jpg"></li>
            <li><img src="images/html_3.jpg"></li>
            <li><img src="images/html_4.jpg"></li>
            <li><img src="images/html_5.jpg"></li>
        </ul>
        <ul class="img-index"></ul>
        <div class="carousel-prev"><img src="./images/left1.png"></div>
        <div class="carousel-next"><img src="./images/right1.png"></div>
    </div>
</div>
<script>
    $(function () {
        $(".carousel-content").carousel({
            carousel: ".carousel",//轮播图容器
            indexContainer: ".img-index",//下标容器
            prev: ".carousel-prev",//左按钮
            next: ".carousel-next",//右按钮
            timing: 3000,//自动播放间隔
            animateTime: 700,//动画时间
            autoPlay: true,//是否自动播放 true/false
            direction: "left",//滚动方向 right/left
        });
        $(".carousel-content").hover(function () {
            $(".carousel-prev,.carousel-next").fadeIn(300);
        }, function () {
            $(".carousel-prev,.carousel-next").fadeOut(300);
        });

        $(".carousel-prev").hover(function () {
            $(this).find("img").attr("src", "images/left2.png");
        }, function () {
            $(this).find("img").attr("src", "images/left1.png");
        });
        $(".carousel-next").hover(function () {
            $(this).find("img").attr("src", "images/right2.png");
        }, function () {
            $(this).find("img").attr("src", "images/right1.png");
        });
    });
</script>

css样式

/*banner*/
.banner {
    --mywidth: 1200px;
    --myheight: 600px;
}

.carousel-content {
    margin: 0 auto;
    width: var(--mywidth);
    height: var(--myheight);
    position: relative;
    overflow: hidden;
    /*border-radius: 10px*/
}

.carousel {
    height: var(--myheight);
    position: absolute
}

.carousel li {
    float: left
}

.carousel li, .carousel li img {
    width: var(--mywidth);
    height: var(--myheight)

}

.img-index {
    position: absolute;
    bottom: 8px;
    width: 100%;
    display: flex;
    justify-content: center
}

.img-index li {
    float: left;
    width: 18px;
    height: 18px;
    line-height: 18px;
    text-align: center;
    border-radius: 10px;
    background-color: rgba(0, 0, 0, .2);
    font-size: 12px;
    cursor: pointer;
    margin: 0 3px;
    color: #fff
}

.img-index li.js_index, .img-index li:hover {
    background-color: rgba(0, 0, 0, .7)
}

.carousel-prev, .carousel-next {
    position: absolute;
    display: inline-block;
    top: 45%;
    cursor: pointer;
    display: none
}

.carousel-prev {
    left: 0
}

.carousel-next {
    right: 0
}

jQuery引入

;(function ($) {
    $.fn.carousel = function (param) {
        var carousel = param.carousel, indexContainer = param.indexContainer, list = $(carousel).children("li"),
            len = list.length, prev = param.prev, next = param.next, timing = param.timing,
            animateTime = param.animateTime, autoPlay = param.autoPlay, timer, index = 1, indexList,
            indexClassName = "js_index", action = true, totalWidtn = list.width() * (list.length + 2),
            direction = param.direction;
        for (var i = 1; i <= list.length; i++) {
            $(indexContainer).append("<li>" + i + "</li>")
        }
        $(carousel).width(totalWidtn).append($(list[0]).clone()).prepend($(list[list.length - 1]).clone()).css("left", "-" + list.width() + "px");
        list = $(carousel).children("li");
        indexList = $(indexContainer).children("li");
        $(indexList[index - 1]).addClass(indexClassName);
        if (autoPlay) {
            startTiming();
            $(carousel + "," + prev + "," + next + "," + indexContainer).hover(function () {
                window.clearInterval(timer);
            }, function () {
                startTiming();
            });
        }

        function startTiming() {
            switch (direction) {
                case "right":
                    timer = window.setInterval("$.rightChangeImg();", timing);
                    break;
                case "left":
                    timer = window.setInterval("$.leftChangeImg();", timing);
                    break;
                default:
                    timer = window.setInterval("$.leftChangeImg();", timing);
            }
        };$.extend({
            changeIndex: function (index) {
                $(indexList).removeClass(indexClassName);
                $(indexList[index]).addClass(indexClassName);
            }
        });
        $.extend({
            leftChangeImg: function () {
                action = false;
                if (index == len) {
                    index = 0;
                    $(carousel).stop(true, true).css("left", "0px");
                }
                index++;
                $(carousel).stop(true, true).animate({left: "-=" + list.width() + "px"}, animateTime);
                setTimeout(function () {
                    if (index == len) {
                        index = 0;
                        $(carousel).stop(true, true).css("left", "0px");
                    }
                    action = true;
                }, animateTime);
                $.changeIndex(index - 1);
            }
        });
        $.extend({
            rightChangeImg: function () {
                action = false;
                if (index == 0) {
                    index = len;
                    $(carousel).stop(true, true).css("left", "-" + left + "px");
                }
                index--;
                var left = totalWidtn - list.width() * 2;
                $(carousel).stop(true, true).animate({left: "+=" + list.width() + "px"}, animateTime);
                setTimeout(function () {
                    if (index == 0) {
                        index = len;
                        $(carousel).stop(true, true).css("left", "-" + left + "px");
                    }
                    action = true;
                }, animateTime);
                if (index == 0) {
                    $.changeIndex(len - 1);
                } else {
                    $.changeIndex(index - 1);
                }
            }
        });
        $(next).on("click", function () {
            var nowLeft = Math.abs(parseInt($(carousel).css("left")));
            var left = totalWidtn - list.width() * 2;
            if (action) {
                if (nowLeft == left) {
                    index = 0;
                    $(carousel).stop(true, true).css("left", "0px");
                }
                $.leftChangeImg();
            }
        });
        $(prev).on("click", function () {
            var nowLeft = Math.abs(parseInt($(carousel).css("left")));
            var left = totalWidtn - list.width() * 2;
            if (action) {
                if (nowLeft == 0) {
                    index = len;
                    $(carousel).stop(true, true).css("left", "-" + left + "px");
                }
                $.rightChangeImg();
            }
        });
        indexList.on("click", function () {
            var no = $(this).index() + 1;
            if (action) {
                if (no > index) {
                    $.changeIndex(no - 1);
                    action = false;
                    var left = (no - index) * list.width();
                    index = no;
                    $(carousel).stop(true, true).animate({left: "-=" + left + "px"}, animateTime);
                    setTimeout(function () {
                        action = true;
                    }, animateTime);
                } else if (no < index) {
                    $.changeIndex(no - 1);
                    action = false;
                    var left = (index - no) * list.width();
                    index = no;
                    $(carousel).stop(true, true).animate({left: "+=" + left + "px"}, animateTime);
                    setTimeout(function () {
                        action = true;
                    }, animateTime);
                }
            }
        });
    }
})(jQuery);

 

  • 0
    点赞
  • 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、付费专栏及课程。

余额充值