【发布时间】:2012-10-14 18:41:54
【问题描述】:
在我学习 javascript 的过程中,我正在制作相同的滑块,但使用 javascript 上的动画(使用 css 这对我来说不是问题 - 它是在网站 google 上制作的),如下所示:
原始 Google 滑块(带有 css 的动画):
http://www.google.com/about/datacenters/inside/
我目前的工作:
<!-- language-all: lang-html -->
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#promo{
display:block;
height: 354px;
width: 790px;
}
.promo-item {
width: 81px;
height: 354px;
float: left;
}
.promo-item.wide {
width: 613px;
}
.threeP {
background-color: rgb(206, 203, 203);
}
.oneP {
background-color: rgb(241, 220, 182);
}
.twoP {
background-color: rgb(187, 217, 226);
}
</style>
</head>
<body>
<div id="promo">
<div class="promo-item twoP wide" id="twoP">
<div>
</div>
</div>
<div class="promo-item threeP" id="threeP">
<div>
</div>
</div>
<div class="promo-item oneP" id="oneP">
<div>
</div>
</div>
</div>
<script type="text/javascript">
var oneP = document.getElementById('oneP');
var twoP = document.getElementById('twoP');
var threeP = document.getElementById('threeP');
step = 1;
function expandPanel1(){
var h = oneP.clientWidth + step;
oneP.style.width = h+"px";
if (h < 614 || h !=614) {
setTimeout("expandPanel1()", 5);
} else { oneP.style.width = 613+"px"; }
}
function expandPanel2(){
var h = twoP.clientWidth + step;
twoP.style.width = h+"px";
if (h < 614 || h !=614) {
setTimeout("expandPanel2()", 5)
} else { twoP.style.width = 613+"px"; }
}
function expandPanel3(){
var h = threeP.clientWidth + step;
threeP.style.width = h+"px";
if (h < 614 || h !=614) {
setTimeout("expandPanel3()", 5)
} else { threeP.style.width = 613+"px"; }
}
//---------------------------------------------
function expandPanel1Minus(){
var h = oneP.clientWidth - step;
oneP.style.width = h+"px";
if (h > 80 || h !=80) {
setTimeout("expandPanel1Minus()", 5)
} else { oneP.style.width = 81+"px"; }
}
function expandPanel2Minus(){
var h = twoP.clientWidth - step;
twoP.style.width = h+"px";
if (h > 80 || h !=80) {
setTimeout("expandPanel2Minus()", 5)
} else { twoP.style.width = 81+"px"; }
}
function expandPanel3Minus(){
var h = threeP.clientWidth - step;
threeP.style.width = h+"px";
if (h > 80 || h !=80) {
setTimeout("expandPanel3Minus()", 5)
} else { threeP.style.width = 81+"px"; }
}
//---------------------------------------------
oneP.onmouseover = function () {
expandPanel1()
expandPanel3Minus()
expandPanel2Minus()
}
twoP.onmouseover = function () {
expandPanel2()
expandPanel3Minus()
expandPanel1Minus()
}
threeP.onmouseover = function () {
expandPanel3()
expandPanel2Minus()
expandPanel1Minus()
}
</script>
我知道这个例子有错误,因为如果用鼠标在滑块上长时间驱动,它会开始“猛烈”运行:) 我故意降低动画速度。
有人可以指导我如何正确实施吗?
【问题讨论】:
-
你完全可以用 css3 做到这一点。为您准备一个示例。
-
@tobias-springer 我知道这可以用 css 完成(对我来说这很简单),但我正在学习 javascript,我很想和他一起做。
-
好的,但是这里是 css 解决方案:jsfiddle.net/aJzgJ/4/embedded/result
-
愿意使用 jQuery 还是应该只使用 javascript?
-
@JFK 没关系。很简单,甚至是对算法的口头描述。我不希望这里的任何人为他增加不必要的工作。如果你准备好参加这个 - 我很高兴。不要把我放在这里给你一个框架,无论如何,这对我来说看起来很愚蠢而且不公平:)
标签: javascript jquery animation slider settimeout