繁体   English   中英

图像无法使用Javascript Image Slider滑动

[英]Images won't slide with Javascript Image Slider

我正在尝试为婚礼网站创建一个简单的图像滑块。 我已经完成了html / css / javascript /。 我已经下载了jQuery并将其链接到HTML上,但是图像不会向左或向右滑动。

目前,我只有4张图片,但想添加其他图片。 两个问题:1)为什么我的图像没有左右滑动。 2)即使我将img或.gallery img中CSS的宽度更改为100%,我似乎也无法使图像完整显示。

**html:**

<!DOCTYPE HTML>
<HTML>
<head>
    <title>Sliding Gallery</title>
    <link href="wedgallery.css" rel="stylesheet">
    <script type = "text/javascript" "src=/js/jquery.js"></script>
    <script type = "text/javascript" "src=/js/script.js"></script>
</head>
<body>
    <div class="gallery-wrapper">
        <div class="gallery-mask">
            <ul class= "gallery-ul">
                <li class= "gallery-li">
                    <img class="gallery-img" src="https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xfa1/t1.0-9/1374070_730708953622490_1010731455_n.jpg"/>
                </li>
                <li class= "gallery-li">
                    <img class="gallery-img" src="https://scontent-a-sea.xx.fbcdn.net/hphotos-xaf1/t1.0-9/10462601_10103161835358498_7988262285040821351_n.jpg"/>
                </li>
                <li class= "gallery-li">
                    <img class="gallery-img" src="https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-xfa1/t1.0-9/10403482_10103161834694828_1487443543209476811_n.jpg"/>
                </li>
                <li class= "gallery-li">
                    <img class="gallery-img" src="https://scontent-a-sea.xx.fbcdn.net/hphotos-xaf1/t1.0-9/10418946_575487062563825_5057353573068803390_n.jpg"/>
                </li>
            </ul>
        </div>

        <div class= "leftbttn">
            <div class= "leftbttn-inner">
            </div>
        </div>
        </div>

        <div class= "rightbttn">
            <div class= "rightbttn-inner">
            </div>
        </div>
    </div>  
</body>
</HTML>

**CSS:**

*
{
    margin:0px;
    padding:0px;
}

a img {
    border:none;

}
body {
    background:#666;
}

.gallery-wrapper {
    width:480px;
    height:360px;
    margin: 50px auto 0 auto;
    border: 3px solid #fff;
    position:relative;
}

.gallery-mask {
    width:480px;
    height:360px;
    overflow:hidden;
}

.gallery-ul {
    list-style-type: none;
    height:360px;
    width:auto;
}

.gallery-li {
    float:left;
    height:360px;
    width:480px;
}

.leftbttn {
    width:75px;
    height:360px;
    position:absolute;
    top:0px;
    left:0px;
    background: #666;
    opacity:0.2;

}

.rightbttn {
    width:75px;
    height:360px;
    position:absolute;
    top:50px;
    right:400px;
    background: #666;
    opacity:0.2;

}


.leftbttn:hover,
.rightbttn:hover {
    opacity:0.5;
}

.rightbttn-inner,
.leftbttn-inner {
    width:35px;
    height:80px;
    margin:140px auto 0 auto;


}

.leftbttn-inner {
    background-image: url('http://findicons.com/files/icons/2766/app_icons/26/arrow_left.png');
    background-repeat: no-repeat;
    background-position: 0px 30px;
}

.leftbttn-inner:hover {
    background-position: -35px 0;

}

.rightbttn-inner {
    background-image: url('http://findicons.com/files/icons/2766/app_icons/26/arrow_right.png');
    background-repeat: no-repeat;
    background-position: 0px 30px;
}

.rightbttn-inner:hover {
    background-position: -35px 0;
}

 **Javascript:**

var numImages = 0;

var currentImage = 1;

totalWidth = 0;

$(document).ready( function(){



    $('.gallery-li').each( function(){
        numImages++;
        totalWidth += 480;
    });

$('.gallery-ul').css('width' , totalWidth + 'px');

$('rightbttn').click( function(){
    moveLeft();
});

$('leftbttn').click( function(){
    moveRight();
});

});

function moveLeft() {
    if(currentImage < numImages)
    {
        $('.gallery-ul').animate( {'marginLeft' : '-=480px'} , 1000 , 'swing')
        currentImage++;
    }
}

function moveRight() {
    if(currentImage > 1)
    {
        $('.gallery-ul').animate( {'marginLeft' : '+=480px'} , 1000 , 'swing')
        currentImage--;
    }
}

您在代码中有一些小错误。 看到小提琴

    $('.rightbttn').click(function() {
        moveLeft();
    });
    $('.leftbttn').click(function() {
        moveRight();
    });
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script>
var numImages = 0;
    var currentImage = 1;
    totalWidth = 0;
    $(document).ready(function() {



        $('.gallery-li').each(function() {
            numImages++;
            totalWidth += 480;
        });
        $('.gallery-ul').css('width', totalWidth + 'px');
        $('.rightbttn').click(function() {
            moveLeft();
        });
        $('.leftbttn').click(function() {
            moveRight();
        });
    });
    function moveLeft() {
        if (currentImage < numImages)
        {
            $('.gallery-ul').animate({'marginLeft': '-=480px'}, 1000, 'swing')
            currentImage++;
        }
    }

    function moveRight() {
        if (currentImage > 1)
        {
            $('.gallery-ul').animate({'marginLeft': '+=480px'}, 1000, 'swing')
            currentImage--;
        }
    }</script>
    <style>
     *
    {
        margin:0px;
        padding:0px;
    }

    a img {
        border:none;
    }
    body {
        background:#666;
    }

    .gallery-wrapper {
        width:1000px;
        height:500px;
        margin: 50px auto 0 auto;
        border: 3px solid #fff;
        position:relative;
    }

    .gallery-mask {
        width:1000px;
        height:500px;
        overflow:hidden;
    }

    .gallery-ul {
        list-style-type: none;
        height:360px;
        width:auto;
    }

    .gallery-li {
        float:left;
        height:360px;
        width:480px;
    }

    .leftbttn {
        width:75px;
        height:495px;
        position:absolute;
        top:0px;
        left:0px;
        background: #666;
        opacity:0.2;
    }

    .rightbttn {
        width:75px;
        height:495px;
        position:absolute;
        top:0px;
        left:925px;
        background: #666;
        opacity:0.2;
    }


    .leftbttn:hover,
    .rightbttn:hover {
        opacity:0.5;
    }

    .rightbttn-inner,
    .leftbttn-inner {
        width:35px;
        height:80px;
        margin:230px auto 0 auto;

    }

    .leftbttn-inner {
        background-image: url('http://findicons.com/files/icons/2766/app_icons/26/arrow_left.png');
        background-repeat: no-repeat;
        background-position: 0px 30px;
    }

    .leftbttn-inner:hover {
        background-position: - 35px 0;
    }

    .rightbttn-inner {
        background-image: url('http://findicons.com/files/icons/2766/app_icons/26/arrow_right.png');
        background-repeat: no - repeat;
        background-position: 0px 30px;
    }

    .rightbttn-inner:hover {
        background-position: -35px 0;
    }
    </style>
</head>

<body>
 <div class = "gallery-wrapper">
            <div class = "gallery-mask" >
                <ul class = "gallery-ul" >
                    <li class = "gallery-li" >
                        <img class="gallery-img" src="https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xfa1/t1.0-9/1374070_730708953622490_1010731455_n.jpg"/>
                    </li>
                    <li class = "gallery-li" >
                        <img class="gallery-img" src="https://scontent-a-sea.xx.fbcdn.net/hphotos-xaf1/t1.0-9/10462601_10103161835358498_7988262285040821351_n.jpg"/>
                    </li>
                    <li class = "gallery-li" >
                        <img class="gallery-img" src="https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-xfa1/t1.0-9/10403482_10103161834694828_1487443543209476811_n.jpg"/>
                    </li>
                    <li class = "gallery-li" >
                        <img class="gallery-img" src="https://scontent-a-sea.xx.fbcdn.net/hphotos-xaf1/t1.0-9/10418946_575487062563825_5057353573068803390_n.jpg"/>
                    </li>
                </ul>
            </div>

            <div class = "leftbttn" >
                <div class = "leftbttn-inner" >
                </div>
            </div>


            <div class = "rightbttn" >
                <div class = "rightbttn-inner" >
                </div>
            </div>
        </div>  
        </body>
</html>

我希望它对您有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM