1

我按照在线教程构建了一个滑块,并让它自己很好地工作。但是,当我从文件中获取代码并将它们传输到我想使用它们的网站时,滑块不起作用。它不显示标题或类似的东西。

这是Javascript:

<script src="jquery-1.4.min.js" type="text/javascript" ></script>

<script type="text/javascript">
$(document).ready(function() {      

//Execute the slideShow, set 4 seconds for each images
slideShow(2000);

});

function slideShow(speed) {


    //append a LI item to the UL list for displaying caption
    $('ul.slideshow').append('<li id="slideshow-caption" class="caption"><div     class="slideshow-caption-container"><h3></h3><p></p></div></li>');

//Set the opacity of all images to 0
$('ul.slideshow li').css({opacity: 0.0});

//Get the first image and display it (set it to full opacity)
$('ul.slideshow li:first').css({opacity: 1.0}).addClass('show');

//Get the caption of the first image from REL attribute and display it
$('#slideshow-caption h3').html($('ul.slideshow li.show').find('img').attr('title'));
$('#slideshow-caption p').html($('ul.slideshow li.show').find('img').attr('alt'));

//Display the caption
$('#slideshow-caption').css({opacity: 0.7, bottom:0});

//Call the gallery function to run the slideshow    
var timer = setInterval('gallery()',speed);

//pause the slideshow on mouse over
$('ul.slideshow').hover(
    function () {
        clearInterval(timer);   
    },  
    function () {
        timer = setInterval('gallery()',speed);         
    }
    );

}

function gallery() {


//if no IMGs have the show class, grab the first image
var current = ($('ul.slideshow li.show')?  $('ul.slideshow li.show') : $('#ul.slideshow li:first'));

//trying to avoid speed issue
if(current.queue('fx').length == 0) {

    //Get next image, if it reached the end of the slideshow, rotate it back to the first image
    var next = ((current.next().length) ? ((current.next().attr('id') == 'slideshow-caption')? $('ul.slideshow li:first') :current.next()) : $('ul.slideshow li:first'));

    //Get next image caption
    var title = next.find('img').attr('title'); 
    var desc = next.find('img').attr('alt');    

    //Set the fade in effect for the next image, show class has higher z-index
    next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);

    //Hide the caption first, and then set and display the caption
    $('#slideshow-caption').slideToggle(300, function () { 
        $('#slideshow-caption h3').html(title); 
        $('#slideshow-caption p').html(desc); 
        $('#slideshow-caption').slideToggle(500); 
    }); 

    //Hide the current image
    current.animate({opacity: 0.0}, 1000).removeClass('show');

    }

}
</script>

这是 HTML 代码

<div id="masthead-wrapper">
    <div id="masthead"></div>
</div>
<div id="navbar-wrapper">
<div id="navbar-left"></div>
<div id="navbar">
    <ul class="nav">
        <li class="navitem"><a href="#">HOME</a></li>
        <li class="navitem"><a href="#">OUR STORY</a></li>
        <li class="navitem"><a href="#">CATERING</a></li>
        <li class="navitem"><a href="#">WHOLESALE</a></li>
        <li class="navitem"><a href="#">CONTACT US</a></li>
    </ul>
</div>
<div id="navbar-right"></div><!-- END NAVIGATION -->
</div>
<div style="clear:both;"></div>
<div id="main-wrapper">
    <div id="main">

        <div id="content">
            <!-- SLIDER -->
            <div id="slider-container">

            <ul class="slideshow">
                <li class="show"><a href="#"><img src="slide-1.jpg" width="618" height="246" title="Delicious Desserts" alt="something funny." /></a></li>
                <li><a href="#"><img src="slide-2.jpg" width="618" height="246" title="Slide 2" alt="Something funny." /></a></li>
                <li><a href="#"><img src="slide-3.jpg" width="618" height="246" title="Slide 3" alt="Something funny." /></a></li>
            </ul>
            </div>

            <!-- END SLIDER -->
        </div><!-- END CONTENT -->
    </div><!-- END MAIN -->
</div><!-- END MAIN WRAPPER -->

<div id="footerwrapper">
<div id="footer"></div>
</div>

最后,CSS

ul.slideshow {
list-style:none; 
width:618px;
height:246px;
overflow:hidden;
position:relative;
margin:0 auto;
padding:0;

}   

ul.slideshow li {
position:absolute;
left:0;
right:0;
}

ul.slideshow li.show {
z-index:500;    
}

ul img {
border:none;    
}

#slideshow-caption {
width:300px;
height:40px;
position:absolute;
bottom:0;
left:0; 
color:#fff;
background:#000;
z-index:500;
margin-bottom: 20px;
margin-left: 20px;
}

#slideshow-caption .slideshow-caption-container {
padding:5px 10px;   
z-index:1000;   
}

#slideshow-caption h3 {
margin-top:5px;
margin-bottom: 5px;
margin-left: 10px;
padding:0;  
font-size:18px;
}

#slideshow-caption p {
margin:5px 0 0 0;
padding:0;
}    

我觉得对于我没有得到的问题有一个非常简单的解决方案。有谁可以帮我离开这里吗?这将不胜感激!

4

1 回答 1

1

您的问题是这条违规行:

timer = setInterval('gallery()',speed);

它应该是:

timer = setInterval(gallery,speed);

这是它的实际操作(没有图片):

http://jsfiddle.net/cFbAd/2/

这是因为setInterval需要对函数的引用,而不是字符串。

于 2012-10-11T21:16:44.727 回答