自己练习做的一个焦点图//焦点图
var timer=null;
function focusTab(nTarget){
window.clearTimeout(timer);
var oDiv=document.getElementById('idFocusImg');
//var goDiv=Math.ceil(Math.abs((nTarget-oDiv.offsetLeft)/10));
if(oDiv.offsetLeft>nTarget){
//oDiv.style.left=oDiv.offsetLeft-goDiv+'px';
oDiv.style.left=oDiv.offsetLeft-20+'px';
timer=window.setTimeout(function(){focusTab(nTarget)},30);
if(oDiv.offsetLeft<=nTarget){
window.clearTimeout(timer);
}
}else{
//oDiv.style.left=oDiv.offsetLeft+goDiv+'px';
oDiv.style.left=oDiv.offsetLeft+20+'px';
timer=window.setTimeout(function(){focusTab(nTarget)},30);
if(oDiv.offsetLeft>=nTarget){
window.clearTimeout(timer);
}
}
}
function focusLeft(){
var oDiv=document.getElementById('idFocusImg');
if(oDiv.offsetLeft==-2784){
oDiv.style.left=0+'px';
}
nTarget=Math.ceil((oDiv.offsetLeft)/928)*928-928;
focusTab(nTarget);
}
function focusReight(){
var oDiv=document.getElementById('idFocusImg');
if(oDiv.offsetLeft==0){
oDiv.style.left=-2784+'px';
}
nTarget=Math.ceil((oDiv.offsetLeft)/928)*928-928;
focusTab(nTarget);
}
/*
//焦点图自动轮播
var nCount=0;
function focusMove(){
if(document.getElementById('idFocusImg').offsetLeft==-2784){
document.getElementById('idFocusImg').style.left=0;
}
focusTab(nCount*-928);
nCount++;
if(nCount==4){
nCount=0;
}
window.setTimeout(focusMove,3000);
}
*/
window.onload=focusMove;
/*焦点图*/
#idFocus{width:928px; height:408px; position:relative; overflow:hidden}
#idFocusImg{width:3712px; position:absolute; left:0; top:0}
#idFocusImg img{float:left}
#idFocus .FocusButton{width:27px; height:27px; background:url(../images/idFocusButton.png) no-repeat; cursor:pointer; z-index:5; position:absolute; top:196px}
#idFocus .FocusLeft{left:10px; background-position:0 0}
#idFocus .FocusRight{right:10px; background-position:-27px 0}
<div id="idFocus">
<div class="FocusButton FocusLeft" onclick="focusLeft()"></div>
<div id="idFocusImg">
<div><img src="temp/focus_01.jpg" width="928" height="408" /></div>
<div><img src="temp/focus_02.jpg" width="928" height="408" /></div>
<div><img src="temp/focus_03.jpg" width="928" height="408" /></div>
<div><img src="temp/focus_01.jpg" width="928" height="408" /></div>
</div>
<div class="FocusButton FocusRight" onclick="focusReight()"></div>
</div>有几点疑问:
1:为什么当我点击onclick="focusReight()"右侧按钮的时候代码不管用?而左侧按钮就管用
2:我为了防止动画运动过界,固设定了
if(oDiv.offsetLeft<=nTarget){
window.clearTimeout(timer);
}为什么动画还是会运动过界?
var timer=null;
function focusTab(nTarget){
window.clearTimeout(timer);
var oDiv=document.getElementById('idFocusImg');
//var goDiv=Math.ceil(Math.abs((nTarget-oDiv.offsetLeft)/10));
if(oDiv.offsetLeft>nTarget){
//oDiv.style.left=oDiv.offsetLeft-goDiv+'px';
oDiv.style.left=oDiv.offsetLeft-20+'px';
timer=window.setTimeout(function(){focusTab(nTarget)},30);
if(oDiv.offsetLeft<=nTarget){
window.clearTimeout(timer);
}
}else{
//oDiv.style.left=oDiv.offsetLeft+goDiv+'px';
oDiv.style.left=oDiv.offsetLeft+20+'px';
timer=window.setTimeout(function(){focusTab(nTarget)},30);
if(oDiv.offsetLeft>=nTarget){
window.clearTimeout(timer);
}
}
}
function focusLeft(){
var oDiv=document.getElementById('idFocusImg');
if(oDiv.offsetLeft==-2784){
oDiv.style.left=0+'px';
}
nTarget=Math.ceil((oDiv.offsetLeft)/928)*928-928;
focusTab(nTarget);
}
function focusReight(){
var oDiv=document.getElementById('idFocusImg');
if(oDiv.offsetLeft==0){
oDiv.style.left=-2784+'px';
}
nTarget=Math.ceil((oDiv.offsetLeft)/928)*928-928;
focusTab(nTarget);
}
/*
//焦点图自动轮播
var nCount=0;
function focusMove(){
if(document.getElementById('idFocusImg').offsetLeft==-2784){
document.getElementById('idFocusImg').style.left=0;
}
focusTab(nCount*-928);
nCount++;
if(nCount==4){
nCount=0;
}
window.setTimeout(focusMove,3000);
}
*/
window.onload=focusMove;
/*焦点图*/
#idFocus{width:928px; height:408px; position:relative; overflow:hidden}
#idFocusImg{width:3712px; position:absolute; left:0; top:0}
#idFocusImg img{float:left}
#idFocus .FocusButton{width:27px; height:27px; background:url(../images/idFocusButton.png) no-repeat; cursor:pointer; z-index:5; position:absolute; top:196px}
#idFocus .FocusLeft{left:10px; background-position:0 0}
#idFocus .FocusRight{right:10px; background-position:-27px 0}
<div id="idFocus">
<div class="FocusButton FocusLeft" onclick="focusLeft()"></div>
<div id="idFocusImg">
<div><img src="temp/focus_01.jpg" width="928" height="408" /></div>
<div><img src="temp/focus_02.jpg" width="928" height="408" /></div>
<div><img src="temp/focus_03.jpg" width="928" height="408" /></div>
<div><img src="temp/focus_01.jpg" width="928" height="408" /></div>
</div>
<div class="FocusButton FocusRight" onclick="focusReight()"></div>
</div>有几点疑问:
1:为什么当我点击onclick="focusReight()"右侧按钮的时候代码不管用?而左侧按钮就管用
2:我为了防止动画运动过界,固设定了
if(oDiv.offsetLeft<=nTarget){
window.clearTimeout(timer);
}为什么动画还是会运动过界?
解决方案 »
- 【紧急求助】Javascript中怎么把array类型转换成object类型
- 代码没问题,但是两个代码在同一个网页中就只能用一个。求大神修改一下....
- 将要打印的 <table> 和表头等放到一个 <DIV> 或 <SPAN> 中作为参数传递过去。
- 用javascript如何把10进制转化为16进制
- <table>的onmousedown怎么不执行?
- 如何在一个js文件里包含另一个文件
- 超难问题!!!如何制作可视化流程设计!!!!??????? (多图)!!!
- <script language="JavaScript" defer>....</script>中defer起什么作用的?
- 请问real player 插件的事件怎么使阿
- ■■请教:如何加密javascript程序呢?并禁止用户下载js■■
- >>>>>>>jstree异步树勾选问题
- jquery EasyUI有没有groupgrid控件
nTarget=Math.ceil((oDiv.offsetLeft)/928)*928-928;
往右改成“+”好试试
nTarget=Math.ceil((oDiv.offsetLeft)/928)*928+928;
<script type="text/javascript"> //焦点图
var timer = null;
function focusTab(nTarget) {
window.clearTimeout(timer);
var oDiv = document.getElementById('idFocusImg');
//var goDiv=Math.ceil(Math.abs((nTarget-oDiv.offsetLeft)/10));
if (oDiv.offsetLeft > nTarget) {
//oDiv.style.left=oDiv.offsetLeft-goDiv+'px';
oDiv.style.left = oDiv.offsetLeft - 20 + 'px';
timer = window.setTimeout(function () { focusTab(nTarget) }, 30);
if (oDiv.offsetLeft <= nTarget) {
window.clearTimeout(timer);
}
} else {
//oDiv.style.left=oDiv.offsetLeft+goDiv+'px';
oDiv.style.left = oDiv.offsetLeft + 20 + 'px';
timer = window.setTimeout(function () { focusTab(nTarget) }, 30);
if (oDiv.offsetLeft >= nTarget-928) {
window.clearTimeout(timer);
}
}
}
function focusLeft() {
var oDiv = document.getElementById('idFocusImg');
if (oDiv.offsetLeft <= -2784+928) {
oDiv.style.left = 928 + 'px';
}
nTarget = Math.ceil((oDiv.offsetLeft) / 928) * 928 - 928;
focusTab(nTarget);
}
function focusReight() {
var oDiv = document.getElementById('idFocusImg');
if (oDiv.offsetLeft >= 0) {
oDiv.style.left = -2784+928 + 'px';
}
nTarget = Math.ceil((oDiv.offsetLeft) / 928) * 928 + 928;
focusTab(nTarget);
}
// //焦点图自动轮播
// var nCount=0;
// function focusMove(){
// if(document.getElementById('idFocusImg').offsetLeft==-2784){
// document.getElementById('idFocusImg').style.left=0;
// }
// focusTab(nCount*-928);
// nCount++;
// if(nCount==4){
// nCount=0;
// }
// window.setTimeout(focusMove,3000);
// }
//
// window.onload = focusMove;
</script>第四张图片
<div><img src="temp/focus_01.jpg" width="928" height="408" /></div>
去掉,宽度才是2784
代码还是有很多不足之处的