jquery控制图片切换

这种js现在用的很多。同时网上的js代码页很多。我直接从网上当了一个来用;代码如下:

html

<div class="bannerbox">
                <div id="focus">
                    <ul>
                        <li><a href="#" target="_blank">
                            <img src="../images/index_18.gif" /></a></li>
                        <li><a href="#" target="_blank">
                            <img src="../images/index_18.gif" /></a></li>
                        <li><a href="#" target="_blank">
                            <img src="../images/index_18.gif" /></a></li>
                        <li><a href="#" target="_blank">
                            <img src="../images/index_18.gif" /></a></li>
                    </ul>
                </div>
            </div>

css:

.bannerbox { width: 970px; height: 447px; overflow: hidden; margin: 0px auto; }
#focus { border:none;width: 970px; height: 447px; clear: both; overflow: hidden; position: relative; float: left;z-index: 10 }
#focus ul { width: 970px; height: 447px; float: left; position: absolute; clear: both; padding: 0px; margin: 0px; border: none}
#focus ul li { float: left; width: 970px; height: 447px; overflow: hidden; position: relative; padding: 0px; margin:0;border: none }
#focus .preNext { width: 550px; height: 440px; position: absolute; top: 2px; cursor: pointer;border: none;}
#focus .pre { left: 0; background: url(../images/sprite.png) no-repeat left center;z-index: 20;border: none; }
#focus .next { right: 0; background: url(../images/sprite1.png) no-repeat right center;z-index: 20;border: none; }

jquery:

$(function () {
    var sWidth = $("#focus").width();
    var len = $("#focus ul li").length;
    var index = 0;
    var picTimer;
    var btn = "<div class=‘btnBg‘></div><div class=‘btn‘>";
    for (var i = 0; i < len; i++) {
        btn += "<span></span>";
    }
    btn += "</div><div class=‘preNext pre‘></div><div class=‘preNext next‘></div>";
    $("#focus").append(btn);
    $("#focus .btnBg").css("opacity", 0);
    $("#focus .btn span").css("opacity", 0.4).mouseenter(function () {
        index = $("#focus .btn span").index(this);
        showPics(index);
    }).eq(0).trigger("mouseenter");
    $("#focus .preNext").css("opacity", 0.0).hover(function () {
        $(this).stop(true, false).animate({ "opacity": "0.5" }, 300);
    }, function () {
        $(this).stop(true, false).animate({ "opacity": "0" }, 300);
    });
    $("#focus .pre").click(function () {
        index -= 1;
        if (index == -1) { index = len - 1; }
        showPics(index);
    });
    $("#focus .next").click(function () {
        index += 1;
        if (index == len) { index = 0; }
        showPics(index);
    });
    $("#focus ul").css("width", sWidth * (len));
    $("#focus").hover(function () {
        clearInterval(picTimer);
    }, function () {
        picTimer = setInterval(function () {
            showPics(index);
            index++;
            if (index == len) { index = 0; }
        }, 2800);
    }).trigger("mouseleave");
    function showPics(index) {
        var nowLeft = -index * sWidth;
        $("#focus ul").stop(true, false).animate({ "left": nowLeft }, 300);
        $("#focus .btn span").stop(true, false).animate({ "opacity": "0.4" }, 300).eq(index).stop(true, false).animate({ "opacity": "1" }, 300);
    }
});

以上代码经测试在ie9以上,ff,chroom下运行正常。代码参考懒人图库

 

jquery控制图片切换,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。