Javascript 鼠标移动上去 滑块跟随效果代码分享_javascript技巧

WBOY
Release: 2016-05-16 17:12:45
Original
1373 people have browsed it

先来一张截图。

Javascript 鼠标移动上去 滑块跟随效果代码分享_javascript技巧

鼠标移动到对应的分类,下面的红色小三角形就会自动跟随,缓慢的跟随。
不管有多少个都可以。
javascript code:

复制代码代码如下:

function changeCoord(id, left) {
$$(id).style.left = left;
}

function $$(id) {
return document.getElementById(id);
}

function $$$(id) {
return document.getElementsByClassName(id)[0];
}

function indexOf(arry, obj) {
for (var i = 0; i if (obj == arry[i]) {
return i;
}
};
}

window.onload = function() {
//给页面上所有的滑块注册事件
//products-box-center 父容器对象

var obj = document.getElementsByClassName('products-box-center');
for(var i=0;i try{
var base=obj[i].getElementsByClassName('products-box-center-title')[0]; //取得每一项标题
var elems=base.getElementsByClassName('products-items-title');
for(var j=0;j var elem=elems[j];
elem.onmousemove=function(){
//获得当前对象的父容器的父容器
var baseElem=this.parentElement.parentElement;
var baseIndex=indexOf(obj,baseElem)+1;

//获得当前对象的坐标
var left = this.offsetLeft;

//获得对应的滑块对象
var slider=$$('products-triangle-'+baseIndex);

//改变滑块的坐标
slider.style.left = left + "px";
//改变当前对象和其他对象的颜色
this.style.color = "red";
//获取当前父容器下面的所有元素
var notes=this.parentElement.getElementsByClassName('products-items-title');
for(var k=0;k if(this!=notes[k])
notes[k].style.color="#666";
}

};
}

}
catch(e){
alert(e);
}
};

}

html code:

复制代码代码如下:



最新商品


笔记本


数码影音


配件


办公打印








上面的html是部分,可以用el表达式循环下,多搞几个。。。
一个上午才做好。。。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!