JavaScript实现图片的浮动效果
2012-06-20 17:46
531 查看
用JavaScript实现图片图片的浮动效果代码如下:
<html xmlns="http://www.w3.org/1999/xhtml">
< head>
< meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
< title>漂浮的图片</title>
< style type="text/css">
div{
position:absolute;
}
< /style>
< /head>
<body>
< div id="divimg">
<img src="2.jpg" width="50" height="90" />
< /div>
< script language="javascript" type="text/javascript">
//获取图片所在的div对象
var divimg=document.getElementById("divimg");
//设置div起始点的位置
var x=0,y=0;
//alert(document.body.offsetHeight);
//var w=document.body.offsetWidth,h=document.body.offsetHeight;
//var w=window.screen.availWidth,h=window.screen.availHeight;
//设置div行进的速度
var xSpeed=2,ySpeed=1;
divimg.style.left=x+"px";
divimg.style.top=y+"px";
var w=document.body.clientWidth-50;
var h=document.body.clientHeight-90;
function floatimg(){
//比较图片是否到达边界,若到达边界则改变方向
if(x>w || x<0) { xSpeed=-xSpeed; }
if(y>h || y<0) { ySpeed=-ySpeed; }
x=x+xSpeed;
y=y+ySpeed;
//设置坐标值,起始坐标+速度
divimg.style.left=x+"px";
divimg.style.top=y+"px";
setTimeout("floatimg()",2);
}
floatimg();
< /script>
< /body>
< /html>
<html xmlns="http://www.w3.org/1999/xhtml">
< head>
< meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
< title>漂浮的图片</title>
< style type="text/css">
div{
position:absolute;
}
< /style>
< /head>
<body>
< div id="divimg">
<img src="2.jpg" width="50" height="90" />
< /div>
< script language="javascript" type="text/javascript">
//获取图片所在的div对象
var divimg=document.getElementById("divimg");
//设置div起始点的位置
var x=0,y=0;
//alert(document.body.offsetHeight);
//var w=document.body.offsetWidth,h=document.body.offsetHeight;
//var w=window.screen.availWidth,h=window.screen.availHeight;
//设置div行进的速度
var xSpeed=2,ySpeed=1;
divimg.style.left=x+"px";
divimg.style.top=y+"px";
var w=document.body.clientWidth-50;
var h=document.body.clientHeight-90;
function floatimg(){
//比较图片是否到达边界,若到达边界则改变方向
if(x>w || x<0) { xSpeed=-xSpeed; }
if(y>h || y<0) { ySpeed=-ySpeed; }
x=x+xSpeed;
y=y+ySpeed;
//设置坐标值,起始坐标+速度
divimg.style.left=x+"px";
divimg.style.top=y+"px";
setTimeout("floatimg()",2);
}
floatimg();
< /script>
< /body>
< /html>
相关文章推荐
- JavaScript实现图片的浮动效果
- 用javascript实现旋转图片效果的代码
- 原生javascript实现图片轮播效果代码
- JS实现图片随机浮动效果
- JavaScript实现图片自动加载的瀑布流效果
- web前端之JavaScript DOM编程艺术之用JavaScript实现动画效果拓展(预览图片)
- Javascript实现经典的图片切换效果
- javascript 实现图片的拖拽和伸缩效果
- 用JavaScript实现图片剪切效果
- 利用javascript从数据库取数据来实现CSDN首页图片的切换效果
- web前端javaScript仿京东淘宝图片鼠标浮动图片上移效果
- JavaScript 浮动定位提示效果实现代码第1/2页
- 【javascript/css】Javascript+Css实现图片滑动浏览效果
- Javascript实现的选择图片效果
- javascript实现图片左右滚动效果【可自动滚动,有左右按钮】
- javascript实现图片滚动效果
- 图片切换效果-Javascript实现
- JavaScript实现图片自动加载的瀑布流效果
- 每日一题_JavaScript.利用纯JavaScript Dom Core实现图片轮播效果?
- 实现多张图片浮动的效果