我正在尝试实现此处找到的旋转jQuery横幅: http://visitmix.com/labs/glimmer/samples/sequence.html
到我的网站上。所有的代码都是布局的,你会认为这是个白痴的证据,但我并没有牢牢掌握如何将语言连接起来。
我现在使用PHP,CSS和HTML非常好,但javaSCRIPT和我似乎没有点击。
我将HTML复制到我的文件中的适当位置,复制了CSS,并将该函数复制到我的js / scripts.js文件中。
这是我的scripts.js文件的样子:
var site = function() {
this.navLi = $('#nav li').children('ul').hide().end();
this.init();
};
site.prototype = {
init : function() {
this.setMenu();
},
// Enables the slidedown menu, and adds support for IE6
setMenu : function() {
$.each(this.navLi, function() {
if ( $(this).children('ul')[0] ) {
$(this)
.append('<span />')
.children('span')
.addClass('hasChildren')
}
});
this.navLi.hover(function() {
// mouseover
$(this).find('> ul').stop(true, true).slideDown('slow', 'easeOutBounce');
}, function() {
// mouseout
$(this).find('> ul').stop(true, true).hide();
});
}
}
new site();
jQuery(function($) {
var timer;
function button1_click(event)
{
$(".slide").css("visibility","hidden");
$("#image1").css("visibility","visible");
$("#image1").css("opacity","0");
$("#image1").animate({"opacity":1},300, "linear", null);
$("ul.buttons li").removeClass("active");
$("#image1").animate({"opacity":1},300, "linear", null);
$("#button1").addClass("active");
clearTimeout(timer);
timer = setTimeout(eval("button2_click"),"3000");
$("#image1").animate({"opacity":1},300, "linear", null);
}
function button2_click(event)
{
$(".slide").css("visibility","hidden");
$("#image2").css("visibility","visible");
$("#image2").css("opacity","0");
$("#image2").animate({"opacity":1},300, "linear", null);
$("ul.buttons li").removeClass("active");
$("#image2").animate({"opacity":1},300, "linear", null);
$("#button2").addClass("active");
clearTimeout(timer);
timer = setTimeout(eval("button3_click"),"3000");
$("#image2").animate({"opacity":1},300, "linear", null);
}
function button3_click(event)
{
$(".slide").css("visibility","hidden");
$("#image3").css("visibility","visible");
$("#image3").css("opacity","0");
$("#image3").animate({"opacity":1},300, "linear", null);
$("ul.buttons li").removeClass("active");
$("#image3").animate({"opacity":1},300, "linear", null);
$("#button3").addClass("active");
clearTimeout(timer);
timer = setTimeout(eval("button1_click"),"3000");
$("#image3").animate({"opacity":1},300, "linear", null);
}
function OnLoad(event)
{
clearTimeout(timer);
timer = setTimeout(eval("button2_click"),"3000");
}
$('#button1').bind('click', button1_click);
$('#button2').bind('click', button2_click);
$('#button3').bind('click', button3_click);
OnLoad();
});
我以为我已经通过编写
将我的scripts.js文件连接到我的HTML文件<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script src="js/scripts.js"></script>
但是当页面加载时,按钮似乎不会在三个图像之间旋转。
有人可以发现我的错误,或者告诉我该怎么做?
编辑:这是我与横幅相关的部分的CSS代码:
/* ROTATING BANNER CSS */
.slideshow {
position:relative;
padding:0;
margin-left: 12.5%;
margin-right: 12.5%;
margin-bottom: 10px;
border-radius: 8px;
overflow: hidden;
width: 1000px;
height: 250px;
}
.slideshow a img {
border:none;
}
.slideshow li.slide {
list-style-type:none;
}
.slideshow .slides {
height:260px;
margin:0;
}
.slideshow .slides li.slide {
visibility:hidden;
position:absolute;
left:0px;
top:0;
}
.slideshow .buttons {
display:none;
}
.slideshow .buttons {
display:block;
position:absolute;
z-index:10;
left:0px;
bottom:20px;
margin:0;
}
.slideshow .buttons li {
float:left;
display:inline;
width:30px;
height:30px;
margin:0;
padding-left:11px;
line-height:30px;
background-image:url('buttonBg.png');
background-repeat:no-repeat;
}
.slideshow .buttons li a {
float:left;
text-decoration:none;
width:30px;
height:30px;
color:#fff;
outline:0;
}
.slideshow ul.buttons li a:hover {
text-decoration:none;
color:#0a0a0a;
}
.slideshow ul.buttons li.active a:hover,
.slideshow ul.buttons li.active a {
color:#666666;
}
这是我的HTML;
<!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 content="text/html; charset=utf-8" http-equiv="Content-Type" />
<link rel="stylesheet" href="css/style.css" type="text/css" />
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,600,400italic,300italic' rel='stylesheet' type='text/css'>
<title>filler.com</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script src="js/scripts.js"></script>
</head>
<body style="background-image: url('images/bg.jpg')">
<br />
<div class="slideshow">
<ul class="buttons">
<li class="active" id="button1"><a href="#" title="warframebuilds">1</a></li>
<li id="button2"><a href="#" title="create build">2</a></li>
<li id="button3"><a href="#" title="forums">3</a></li>
</ul>
<ul class="slides">
<li style="visibility:visible" class="slide" id="image1">
<a href="http://www.filler.com/"><img alt="warframebuilds" src="images/bannerimg1.png" /></a></li>
<li class="slide" id="image2">
<a href="http://www.filler.com/create.php"><img alt="create build" src="images/bannerimg2.png" /></a></li>
<li class="slide" id="image3">
<a href="http://www.filler.com/forums"><img alt="forums" src="images/bannerimg3.png" /></a></li>
</ul>
</div>
非常感谢!
答案 0 :(得分:0)
您可能需要交换两行:
jQuery(function($) { // short for jQuery(document).ready(function ($) {
new site();
var timer;
// ...
在您的代码中,new site()
超出了ready事件。因此,如果您的代码位于HTML的开头,那么这些元素尚未准备就绪,因此您的代码无效。
关于使用setTimeout
的注意事项:您的示例源做了一些丑陋的事情,不需要eval
和隐式字符串到数字转换。只是做
timer = setTimeout(button2_click, 3000);