javascript代码实例教程-Jquery编程开发如何实现图片轮换

发布时间:2018-12-29 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-Jquery编程开发如何实现图片轮换脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。

网站首页没有一点动画怎么可以,我以前用过Flash As3做过图片切换,效果非常不错,可是麻烦,改变起来麻烦。一直都想自己做个图片切换效果,总认为比较麻烦,今天自己实践了一下,其实还比较简单。不过有个小问题,IE8不兼容模式下 设置有透明效果的p 样式添加失效了,但是我用谷歌,IE8兼容测试都ok。

反正是给自己记录的,也不多话了,js没有与页面分离,也没有做出插件。一个网站要不了几个这种效果,先实现了再说吧。最后的效果还是很高大上的。

页面+JS代码

代码如下:


<script type="text/javascript">
        var picCurrent = 1;
        var picTotal = 8;
        var interval; //自动运行
        function picChange(current) {
            //停止当前动画
            if ($("#pImg").is(":animated")) { $("#pImg").stop(); }
            picCurrent = current;
            //为当前选择的设置样式
            $("#pLink").find("a").removeClass("picselect")
            $("#pLink").find("a[title='" + picCurrent + "']").addClass("picselect");
            //设置下面的图片说明
            var remark = "<a href=/"images/pic" + picCurrent + ".jpg/">";
            switch (picCurrent) {
                case 1: remark += " 菊花〔拉丁学名:Dendranthema morifolium(Ramat. )Tzvel.〕,常用chrysanthemum。菊花是菊科,菊属多年生草本... "; break;
                default: remark += picCurrent + "测试说明"; break;
            }
            remark += "</a>";
            $("#picremark").html(remark);
            //运行动画
            $("#pImg").animate({ left: -((picCurrent - 1) * 1000) + "px" }, "1000");
            return false;
        }
        //暂不需使用
        function PicPer() {
            if (picCurrent > 1) {
                picCurrent--;
            }
            else {
                picCurrent = picTotal;
            }
            picChange(picCurrent);
        }
        //下一张
        function PicNext() {
            if (picCurrent == picTotal) {
                picCurrent = 1
            }
            else {
                picCurrent++;
            }
            picChange(picCurrent);
        }
        //自动切换图片
        function PicRun(functionName) {
            picChange(1);
            interval = setInterval(PicNext, "3000");
        }
        $(document).ready(function () {
            PicRun();
        });
    </script>


 

代码如下:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
    <title>图片切换</title>
    <script src="jquery-1.8.0.js" type="text/javascript"></script>
    <link href="picchange.css" rel="stylesheet" type="text/css" />
    </head>
<body>
    <p class="picMain">
        <p class="picimg" id="pImg">
            <img src="images/pic1.jpg" class="pic" />
            <img src="images/pic2.jpg" class="pic" />
            <img src="images/pic3.jpg" class="pic" />
            <img src="images/pic4.jpg" class="pic" />
            <img src="images/pic5.jpg" class="pic" />
            <img src="images/pic6.jpg" class="pic" />
            <img src="images/pic7.jpg" class="pic" />
            <img src="images/pic8.jpg" class="pic" />
        </p>
        <p class="picaction" id="pLink">
            <a href="images/pic8.jpg" title="8" onclick=" return picChange(8)" class="">8</a> <a href="images/pic7.jpg" title="7" onclick=" return picChange(7)">7</a> <a href="images/pic6.jpg" title="6"
                    onclick=" return picChange(6)">6</a> <a href="images/pic5.jpg" title="5" onclick=" return picChange(5)">
                        5</a> <a href="images/pic4.jpg" title="4" onclick=" return picChange(4)">4</a>
            <a href="images/pic3.jpg" title="3" onclick=" return picChange(3)">3</a> <a href="images/pic2.jpg"
                title="2" onclick=" return picChange(2)">2</a> <a href="images/pic1.jpg" title="1"
                    onclick=" return picChange(1)" class="">1</a>
        </p>
        <p id="picremark" class="picRemark">
            测试介绍文件了啊</p>
    </p>
</body>
</html>

 css的实现

代码如下:


.picMain
{
    margin: auto;
    overflow: hidden;
    width: 1000px;
    height: 400px;
    position: relative;
}
.picimg
{
    width: 10000px;
    height: 400px;
    background-color: #000000;
    position: absolute;
    top: 0px;
}
.picRemark
{
    position: absolute;
    width: 500px;
    height: 50px;
    bottom: 0px;
    left: 0px;
    color: #FFFFFF;
    text-indent: 2em;
}
.picRemark a
{
    color: #FFFFFF;
    text-decoration: none;
}
.picRemark a:hover
{
    text-decoration: underline;
}
.picaction
{
    position: absolute;
    width: 1000px;
    height: 50px;
    background-color: #000000;
    filter: alpha(opacity=50);
    -moz-opacity: 0.5;
    opacity: 0.5;
    overflow: auto;
    bottom: 0px;
    left: 0px;
    text-align: right;
}
.picaction a
{
    border: 1px solid #C0C0C0;
    width: 30px;
    height: 30px;
    float: right;
    line-height: 30px;
    text-decoration: none;
    text-align: center;
    color: #FFFFFF;
    font-weight: bold;
    margin-top: 10px;
    display: block;
    margin-right: 10px;
}
.pic
{
    width: 1000px;
    height: 400px;
    float: left;
}
.picselect
{
    background-color: #919191;
}

觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!

脚本宝典总结

以上是脚本宝典为你收集整理的javascript代码实例教程-Jquery编程开发如何实现图片轮换全部内容,希望文章能够帮你解决javascript代码实例教程-Jquery编程开发如何实现图片轮换所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:CSSHTML