当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JS焦点图切换,上下翻转

JS焦点图切换,上下翻转

2019年07月19日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:

<!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 http-equiv="content-type" content="text/html; charset=utf-8" />
<title>mypaly</title>
<style type="text/css">
* { margin:0; padding:0;}
ul, li { list-style:none;}
body{
text-align:center;
}
#play{
width:400px;
height:300px;
position:absolute;
left:50%;
top:50%;
margin:-155px 0 0 -205px;
overflow:hidden;

}
#playimg{
width:400px;
height:300px;
position:absolute;
}
#playimg li{
height:300px;
overflow:hidden;
}
#playimg img{
width:400px;
height:300px;
}
#playbtn{
position:absolute;
left:0;
bottom:5px;
}
#playbtn li{
display:inline;
background:#eee;
padding:2px 5px;
margin:0 3px;
cursor:pointer;
}
#playbtn .current{
background:#f0f;
}
</style>
<script type="text/javascript">
function $(id){return document.getelementbyid(id)}
function moveelement(elementid,final_x,final_y,interval) {//这个方法在dom艺术那个书上讲的很清楚
if (!document.getelementbyid) return false;
if (!document.getelementbyid(elementid)) return false;
var elem = document.getelementbyid(elementid);
if (elem.movement) {
cleartimeout(elem.movement);
}
if (!elem.style.left) {
elem.style.left = "0px";
}
if (!elem.style.top) {
elem.style.top = "0px";
}
var xpos = parseint(elem.style.left);
var ypos = parseint(elem.style.top);
if (xpos == final_x && ypos == final_y) {
return true;
}
if (xpos < final_x) {
var dist = math.ceil((final_x - xpos)/10);
xpos = xpos + dist;
}
if (xpos > final_x) {
var dist = math.ceil((xpos - final_x)/10);
xpos = xpos - dist;
}
if (ypos < final_y) {
var dist = math.ceil((final_y - ypos)/10);
ypos = ypos + dist;
}
if (ypos > final_y) {
var dist = math.ceil((ypos - final_y)/10);
ypos = ypos - dist;
}
elem.style.left = xpos + "px";
elem.style.top = ypos + "px";
var repeat = "moveelement('"+elementid+"',"+final_x+","+final_y+","+interval+")";
elem.movement = settimeout(repeat,interval);
}
function imgchange(num){//切换图片焦点
if(!$('play')) return false;
var piclist=$('playimg').getelementsbytagname('img');
var imgheight=piclist[0].offsetheight;
var movey=-(imgheight*num);
moveelement('playimg',0,movey,5);
}
function classtoggle(arr,n){//切换按钮样式
for(var i=0;i<arr.length;i++){
arr[i].classname='';
}
arr[n].classname='current';
}
function btnchange(btns){//鼠标移动播放
if(!$(btns)) return false;
$('play').onmouseover = function(){autokey = false};
$('play').onmouseout = function(){autokey = true};
var arr=$(btns).getelementsbytagname('li');
for(var i=0;i<arr.length;i++){
arr[i].index=i;//设置索引号
arr[i].onmouseover=function(){
//var num=index(this,arr);
classtoggle(arr,this.index);
imgchange(this.index);
}
}
}
function autochange(btns){
if(!$(btns)) return false;
if(!autokey) return false;
var arr=$(btns).getelementsbytagname('li');
for(var i=0;i<arr.length;i++){
if(arr[i].classname=='current'){
var n=i+1;
}
}
if(n>=arr.length) n=0;
classtoggle(arr,n);
imgchange(n);
}
var autokey = true;
setinterval("autochange('playbtn')",3000);
window.onload=function(){
btnchange('playbtn');
}
</script>
</head>

<body>
<div id="play">
<ul id="playimg">
<li><img src="../images/blue hills.jpg" alt="" /></li>
<li><img src="../images/sunset.jpg" alt="" /></li>
<li><img src="../images/water lilies.jpg" alt="" /></li>
<li><img src="../images/winter.jpg" alt="" /></li>
</ul>
<ul id="playbtn"><li class="current">1</li><li>2</li><li>3</li><li>4</li></ul>
</div>
</body>
</html>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网