js实现一个div缓缓移动到某一个位置再缓缓回到原来位置
2017-08-07 19:08
676 查看
这是js中onmouseover 和onmouseout 的一个练习题,前几天写的,写出来后div却频频抖动,和同学一起研究解决了这个问题。
现在我把代码贴出来,希望可以帮到遇到同样问题的童鞋!
效果图大致如下:
代码如下:
<style>
#father{
width: 150px;
height: 200px;
position: absolute;
background: blue;
top:100px;
left:-150px;
}
#son{
width: 20px;
height: 80px;
background: green;
color:white;
position: absolute;
top:60px;
left:150px;
}
</style>
<body>
<div id="father">
<div id = "son">分享到</div>
</div>
<script>
window.onload = function(){
var ofa = document.getElementById("father");
var oson = document.getElementById("son");
var change=null;
function move(speed){
clearInterval(change);
change=setInterval(function(){
if(speed>0){
if(ofa.offsetLeft>-1 ){
clearInterval(change);
}else{
ofa.style.left=ofa.offsetLeft+speed+'px';
}
}else{
if(ofa.offsetLeft<-149){
clearInterval(change);
}else{
ofa.style.left=ofa.offsetLeft+speed+'px';
}
}
},30)
}
ofa.onmouseover=function(){
move(10);
}
ofa.onmouseout=function(){
move(-10);
}
/*ofa.onmouseover = function(e){
if (!e) e = window.event;
var reltg = e.relatedTarget ? e.relatedTarget : e.fromElement;
while (reltg && reltg != this) reltg = reltg.parentNode;
if (reltg != this) {
clearInterval(change);
change = setInterval(function(){
var vashed = 10;
if(ofa.offsetLeft>-10){
clearInterval(change);
}else{
ofa.style.left = ofa.offsetLeft+vashed +"px";
}
},30)
}
}
ofa.onmouseout = function(e){
if (!e) e = window.event;
var reltg = e.relatedTarget ? e.relatedTarget : e.toElement;
while (reltg && reltg != this) reltg = reltg.parentNode;
if (reltg != this) {
// clearInterval(transform);
transform = setInterval(function(){
var chased =-10;
if(ofa.offsetLeft<-140){
clearInterval(transform);
}else{
ofa.style.left = ofa.offsetLeft+chased +"px";
}
},30)
}
}*/
}
</script>
</body>
现在我把代码贴出来,希望可以帮到遇到同样问题的童鞋!
效果图大致如下:
代码如下:
<style>
#father{
width: 150px;
height: 200px;
position: absolute;
background: blue;
top:100px;
left:-150px;
}
#son{
width: 20px;
height: 80px;
background: green;
color:white;
position: absolute;
top:60px;
left:150px;
}
</style>
<body>
<div id="father">
<div id = "son">分享到</div>
</div>
<script>
window.onload = function(){
var ofa = document.getElementById("father");
var oson = document.getElementById("son");
var change=null;
function move(speed){
clearInterval(change);
change=setInterval(function(){
if(speed>0){
if(ofa.offsetLeft>-1 ){
clearInterval(change);
}else{
ofa.style.left=ofa.offsetLeft+speed+'px';
}
}else{
if(ofa.offsetLeft<-149){
clearInterval(change);
}else{
ofa.style.left=ofa.offsetLeft+speed+'px';
}
}
},30)
}
ofa.onmouseover=function(){
move(10);
}
ofa.onmouseout=function(){
move(-10);
}
/*ofa.onmouseover = function(e){
if (!e) e = window.event;
var reltg = e.relatedTarget ? e.relatedTarget : e.fromElement;
while (reltg && reltg != this) reltg = reltg.parentNode;
if (reltg != this) {
clearInterval(change);
change = setInterval(function(){
var vashed = 10;
if(ofa.offsetLeft>-10){
clearInterval(change);
}else{
ofa.style.left = ofa.offsetLeft+vashed +"px";
}
},30)
}
}
ofa.onmouseout = function(e){
if (!e) e = window.event;
var reltg = e.relatedTarget ? e.relatedTarget : e.toElement;
while (reltg && reltg != this) reltg = reltg.parentNode;
if (reltg != this) {
// clearInterval(transform);
transform = setInterval(function(){
var chased =-10;
if(ofa.offsetLeft<-140){
clearInterval(transform);
}else{
ofa.style.left = ofa.offsetLeft+chased +"px";
}
},30)
}
}*/
}
</script>
</body>
相关文章推荐
- css,js实现在鼠标移动到一个位置在这个位置下面显示一个已经存在的div,这个div的位置随意
- js实现div固定在屏幕的某一个位置
- 页面的div中有滚动条,js实现刷新页面后回到记录时滚动条的位置
- JS获取div滚动条距离实现弹出标签位置动态移动
- JS+CSS实现几个DIV层上下移动交换位置的效果
- Joomla!中如何使用switcher.js实现一个位置上div层的切换显示 【转】
- js实现图片在div容器内移动的网页特效代码
- Js无刷新添加新层,拖动DIV层可互换位置的JavaScript实现
- js实现键盘操作实现div的移动或改变的原理及代码
- JS简单实现DIV相对于浏览器固定位置不变的方法
- [Selenium]通过Selenium实现在当前浏览器窗口点击一个图标之后,弹出另外一个窗口,关闭这个窗口,再回到原来的窗口进行操作
- 在Android中动画移动一个View的位置,采用Scroller类实现Android动画之 View移动
- js实现键盘操作对div的移动或改变-------Day43
- JS图像映射的应用,在一个图片点击或移动到不同位置有不同响应
- [Selenium]通过Selenium实现在当前浏览器窗口点击一个图标之后,弹出另外一个窗口,关闭这个窗口,再回到原来的窗口进行操作
- js实现键盘控制DIV移动的方法
- js控制div样式显示与隐藏,JS通过点击超链接右边(指定位置)显示一个图标
- js实现浮动框跟随页面滚动,最后停留在原来位置
- js 实现移动div窗体
- 17. 用JavaScript实现如下效果:一个div的宽度从其初始宽度1px,每秒增长1px,直到变到100px,再从100px每秒减少1px,直到变回到1px,如此反复5次后,该变化停止,并将其最