求一个javascript控制div左右移动效果!!源码

hongcha99 2012-02-15 09:29:59
我要一种如: 北京,天津,上海,杭州,苏州

< 天津 上海 北京 >

点击<或者>可以实现左右滚动,我要javascript和div的,不要ul li 的

求源码
...全文
365 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
licip 2012-02-16
  • 打赏
  • 举报
回复
建议用jquery去实现吧。
oggmm 2012-02-16
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 hongcha99 的回复:]

确实不行,如果超出你的宽度不会隐藏而会换行。

北京 天津 上海 北京 天津 上海 北京 天津 上海 北京 天津 上海


如果这么多内容的就换行了
[/Quote]

我把它换成你上面的也一样没问题,检查一下你自己设置的样式吧
xiongxyt2 2012-02-16
  • 打赏
  • 举报
回复
hongcha99 2012-02-16
  • 打赏
  • 举报
回复
确实不行,如果超出你的宽度不会隐藏而会换行。

北京 天津 上海 北京 天津 上海 北京 天津 上海 北京 天津 上海


如果这么多内容的就换行了
dongfangzhuli 2012-02-15
  • 打赏
  • 举报
回复
上网搜一下类似的
hongcha99 2012-02-15
  • 打赏
  • 举报
回复
我靠人都哪里去了啊
hongcha99 2012-02-15
  • 打赏
  • 举报
回复
谁来告诉我啊
hongcha99 2012-02-15
  • 打赏
  • 举报
回复
自己顶起来
oggmm 2012-02-15
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 hongcha99 的回复:]

楼上的不管用啊
[/Quote]

怎么不管用了?左右你都点过了?刚开始是最左边所以点右不让它东,你先点了左再点右看看
未知数 2012-02-15
  • 打赏
  • 举报
回复
这个是有动画效果的
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<script type="text/javascript">
function moveDiv(fx,distance)//fx是移动方向,distance为移动距离
{
var obj=document.getElementById('div1');
var nowleft=parseInt(obj.style.left);
var step=5;
if(distance-step<0) step=distance;
if(fx=='left')
{
obj.style.left=nowleft- step;
}
if(fx=='right')
{
obj.style.left=nowleft+ step;
}
distance-=step;
if(distance==0) return;
setTimeout(function(){moveDiv(fx,distance);},10);
}
</script>
<form id="form1" runat="server">
<div>
<div id='div1' style="width:150px; position:absolute;top:100px;left:100px;">
北京 天津 上海
</div>
<input type="button" value="左" onclick="moveDiv('left',100)" />
<input type="button" value="右" onclick="moveDiv('right',100)" />
</div>
</form>
</body>
</html>
未知数 2012-02-15
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 hongcha99 的回复:]
差不多但不是我完全想要的,我想要点一下直接过去3个或者4个的那种
[/Quote]
如果只是动作大点,改变一下每次移动的像素就行了
做好就需要写动画效果了,略微麻烦一点
hongcha99 2012-02-15
  • 打赏
  • 举报
回复
差不多但不是我完全想要的,我想要点一下直接过去3个或者4个的那种
未知数 2012-02-15
  • 打赏
  • 举报
回复
简单的,自己优化
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<script type="text/javascript">

function moveDiv(fx)
{
var obj=document.getElementById('div1');
var nowleft=parseInt(obj.style.left);
if(fx=='left')
{
obj.style.left=nowleft+ 5;
}
if(fx=='right')
{
obj.style.left=nowleft- 5;
}
}
</script>
<form id="form1" runat="server">
<div>
<div id='div1' style="width:150px; position:absolute;top:100px;left:100px;">
北京 天津 上海
</div>
<input type="button" value="左" onclick="moveDiv('left')" />
<input type="button" value="右" onclick="moveDiv('right')" />
</div>
</form>
</body>
</html>
hongcha99 2012-02-15
  • 打赏
  • 举报
回复
楼上的不管用啊
oggmm 2012-02-15
  • 打赏
  • 举报
回复

<!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>test</title>
<script language="javascript" type="text/javascript">
var scrollFun = function (dir) {
var box = document.getElementById("box");
var con = document.getElementById("con");
var boxWidth = box.offsetWidth;
var conWidth = con.offsetWidth;
var left = +con.style.left.replace("px", "");
if (dir === "left") {
if (left <= boxWidth - conWidth) {
return;
}
con.style.left = (left - 40) + "px";
}
else if (dir === "right") {
if (left === 0) {
return;
}
con.style.left = (left + 40) + "px";
}

};
window.onload = scrollFun;
</script>
<style type="text/css">
.box{ border:1px solid red; width:40px; height:50px; position:relative; overflow:hidden;}
.con{ position:absolute; top:20px; white-space:nowrap;}
</style>
</head>

<body>
<input type="button" onclick="scrollFun('left')" value="左" />
<input type="button" onclick="scrollFun('right')" value="右" />
<div id="box" class="box">
<div id="con" class="con" >滚动1滚动2滚动3滚动4滚动5滚动6滚动7滚动8滚动9</div>
</div>
</body>
</html>

87,990

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧