自定义jQuery图像滑块不工作

时间:2015-03-26 18:01:50

标签: jquery loops slider

无法显示任何图片。

JQUERY

function slider() {
var slideIndex = 1;
var slideLength = $('#slider').length();
function slide() {
    if (slideIndex < 1) {
        slideIndex = (slideLength - 1);
    }

    if (slideIndex > slideLength) {
        slideIndex = 1;
    }

    $('#slider:nth-child(' + slideIndex + ')').css({
        opacity: 0
    });
    $('#slider:nth-child(' + (slideLength + 1) + ')').css({
        opacity: 1
    })
}
if (!$('#slider').is(':hover')) {
    timer = setInterval(function () {
        slideIndex++;
        slide();
    });
}

$('.home_img_slider_btn_left').on('click', function () {
    slideIndex--;
    slide();
});
$('.home_img_slider_btn_right').on('click', function () {
    slideIndex++;
    slide();
});
}

HTML

    <div style="" id="home_container">
    <div class="page_title">
        <i></i>Home
    </div>
    <div class="home_img_slider_container">
        <div class="home_img_slider">
            <div class="home_img_slider_btn_left">
                <img src="files/img/icon-arrow-left.png" />
            </div>
            <div id="slider">
                <img title="Eaglespeed" class="home_img_slider_img one" src="files/img/eaglespeed-corner.jpg" />
                <img title="Eaglespeed" class="home_img_slider_img two" src="files/img/eaglespeed.jpg" />
                <img title="Eaglespeed" class="home_img_slider_img three" src="files/img/eaglespeed-front.jpg" />
                <img title="Eaglespeed" class="home_img_slider_img four" src="files/img/eaglespeed-giants.jpg" />
                <img title="Eaglespeed" class="home_img_slider_img five" src="files/img/eaglespeed-sign.jpg" />
            </div>
            <div class="home_img_slider_btn_right">
                <img src="files/img/icon-arrow-right.png" />
            </div>
        </div>
    </div>
</div>

CSS

.home_img_slider_container {
width: 90%;
margin: 0 5% 0 5%;
}

.home_img_slider_btn_left, .home_img_slider_btn_right {
cursor: pointer;
opacity: 0;
position: absolute;
width: 50px;
height: 50px;
top: calc(50% - 50px);
background-color: #c2f8ff;
border-radius: 50%;
-moz-transition: opacity .5s ease, top .5s ease, background-color .5s ease;
-o-transition: opacity .5s ease, top .5s ease, background-color .5s ease;
-webkit-transition: opacity .5s ease, top .5s ease, background-color .5s ease;
transition: opacity .5s ease, top .5s ease, background-color .5s ease;
z-index: 3;
}

.home_img_slider_btn_left {
left: 10px;
}

.home_img_slider_btn_left:hover {
    background-color: rgba(194, 248, 255, 0.50);
}

.home_img_slider_btn_left img {
    height: 50px;
}

.home_img_slider {
margin-left: auto;
margin-right: auto;
width: 800px;
height: 600px;
overflow: hidden;
position: relative;
border: 1px solid #1F242A;
}

#slider img {
    height: 100%;
    width: 100%;
    opacity: 0;
    -moz-transition: opacity .5s ease;
    -o-transition: opacity .5s ease;
    -webkit-transition: opacity .5s ease;
    transition: opacity .5s ease;
}

.home_img_slider:hover .home_img_slider_btn_left, .home_img_slider:hover .home_img_slider_btn_right {
    opacity: 1;
    top: calc(50% - 25px);
}

.home_img_slider_btn_right {
right: 10px;
}

.home_img_slider_btn_right:hover {
    background-color: rgba(194, 248, 255, 0.50);
}

.home_img_slider_btn_right img {
    height: 50px;
}

没有任何图片出现或过渡,我花了好几个小时试图解决这个问题。我认为它与第一次自动转换有关,然后它就无法切换。

更新1 我在第一次回复后查看了我的代码,并将.animate更改为.css,意识到我正在更改图片&#39;不透明度的顺序相反。还是行不通。

2 个答案:

答案 0 :(得分:0)

您可以为每个包含它们的图像或元素指定相同的类。然后你可以抓住那个系列的长度。此外,您应该默认显示系列中的第一个。昨天我正在愚弄一种方法来做这样的事情,这就是我所拥有的,让你知道它是如何工作的:

<button id="right">next</button>
<hr>

<div class="hidey" id="pic1">1</div>
<div class="hidey" id="pic2">2</div>
<div class="hidey" id="pic3">3</div>
<div class="hidey" id="pic4">4</div>

JS:

$(function(){
     var i = 1;
     var x = $(".hidey");
     var y = x.length;

    //shows the first image before any clicking:
     $(x[0]).show();

     $("#right").click(function() {

         if(i >= y) {
          i = 0;
         } 

     //HIDES the first pic, and all others except current index:

     $(".hidey").not( $(x)[ i ] ).hide();

    //Shows the index assigned in the first line, which is 1, which is actually the SECOND pic in the series.     

     $(x[i]).show();

    //...and the next and on.    
     i++; 
     })

});

http://jsfiddle.net/toL33fzy/5/

答案 1 :(得分:0)

试试我的代码:

&#13;
&#13;
$(document).ready(function(){
    var slideIndex = 1;
    var slideLength = $('#slider > img').size();
    var timer;
    
    function slide() {
        if (slideIndex < 0) {
            slideIndex = (slideLength - 1);
        }
    
        if (slideIndex >= slideLength) {
            slideIndex = 0;
        }
        
        $('#slider > img').hide().animate({
        opacity: 0
    }, 200);
        $('#slider > img').eq(slideIndex).show().animate({
        opacity: 1
    }, 200);
    }
    
    function change(){
        timer = setInterval(function () {
        slideIndex++;
        slide();
    },1000);
    }
    
    if (!$('.home_img_slider_container').is(':hover')) {
        change();
    }
    
    $('.home_img_slider_container').mouseover(function(){
     clearInterval(timer);
    }).mouseout (function(){
         change();
    });
    
    $('.home_img_slider_btn_left').on('click', function () {
        slideIndex--;
        slide();
    });
    $('.home_img_slider_btn_right').on('click', function () {
        slideIndex++;
        slide();
    });
    
});
&#13;
.home_img_slider_container {
width: 200px;
margin: 0 5% 0 5%;
}

.home_img_slider_btn_left, .home_img_slider_btn_right {
cursor: pointer;
opacity: 0;
position: absolute;
width: 50px;
height: 50px;
top: calc(50% - 50px);
background-color: #c2f8ff;
border-radius: 50%;
-moz-transition: opacity .5s ease, top .5s ease, background-color .5s ease;
-o-transition: opacity .5s ease, top .5s ease, background-color .5s ease;
-webkit-transition: opacity .5s ease, top .5s ease, background-color .5s ease;
transition: opacity .5s ease, top .5s ease, background-color .5s ease;
z-index: 3;
}

.home_img_slider_btn_left {
left: 10px;
}

.home_img_slider_btn_left:hover {
    background-color: rgba(194, 248, 255, 0.50);
}

.home_img_slider_btn_left img {
    height: 50px;
}

.home_img_slider {
margin-left: auto;
margin-right: auto;
width: 400px;
height: 400px;
overflow: hidden;
position: relative;
border: 1px solid #1F242A;
}

#slider img {
    opacity: 0;
    -moz-transition: opacity .5s ease;
    -o-transition: opacity .5s ease;
    -webkit-transition: opacity .5s ease;
    transition: opacity .5s ease;
    width:400px;
    height:400px;
    display:none;
}

.home_img_slider:hover .home_img_slider_btn_left, .home_img_slider:hover .home_img_slider_btn_right {
    opacity: 1;
    top: calc(50% - 25px);
}

.home_img_slider_btn_right {
right: 10px;
}

.home_img_slider_btn_right:hover {
    background-color: rgba(194, 248, 255, 0.50);
}

.home_img_slider_btn_right img {
    height: 50px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div style="" id="home_container">
    <div class="page_title">
        <i></i>Home
    </div>
    <div class="home_img_slider_container">
        <div class="home_img_slider">
            <div class="home_img_slider_btn_left">
                <img src="http://www.mricons.com/store/png/121768_40595_32_arrow_left_icon.png" />
            </div>
            <div id="slider">
                                
                <img title="Eaglespeed" src="http://glamorouslymommy.com/wp-content/uploads/2013/05/small-background1.jpg" />
                
                 <img title="Eaglespeed"  src="http://images.neopets.com/ncmall/claw/popups/bg.png" />
                
                <img title="Eaglespeed"  src="http://botanical.bg/en/wp-content/uploads/2014/05/400px-Organic-Logo.svg_.png" />
                
            </div>
            <div class="home_img_slider_btn_right">
                <img src="http://cdn-img.easyicon.net/png/10967/1096749.gif" />
            </div>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;