js学习 无缝滚动 和 随机色

<!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=gb2312" />
<title>无缝滚动</title>
<style>
* {margin:0; padding:0;}
#div {width:712px; height:108px; margin:100px auto; position:relative; background:red; overflow:hidden;}
#div ul {position:absolute; left:0; top:0;}
#div ul li {float:left; width:178px; height:108px; list-style:none;}
img{width:178px; height:108px;}

</style>
<script>
window.onload=function(){
var div=document.getElementById("div");
//var col=document.getElementById("col");
var ul=document.getElementsByTagName("ul")[0];
var li=document.getElementsByTagName("li");
ul.innerHTML+=ul.innerHTML;
ul.style.width=li[0].offsetWidth*li.length+"px";
var speed=-2;
document.getElementsByTagName("a")[0].onclick=function(){
speed=-2;
}
document.getElementsByTagName("a")[1].onclick=function(){
speed=2;
}
function move(){
if(ul.offsetLeft<-ul.offsetWidth/2){
ul.style.left="0"
}
if(ul.offsetLeft>0){
ul.style.left=-ul.offsetWidth/2+"px";
}
ul.style.left=ul.offsetLeft+speed+"px";
}
var timer=setInterval(move,30);
div.onmouseover=function(e){
clearInterval(timer);
}
div.onmouseout=function(){
timer=setInterval(move,30);
}
}

</script>
<script>

function randomColor(){
var rand="#"+Math.floor(Math.random() * 0xFFFFFF).toString(16);

if(rand.length == 7){
//alert(rand);
var colo=document.getElementById("col");
colo.innerHTML=rand;
document.getElementById("col").style.backgroundColor=rand;
return rand;
}else{
return randomColor();
}

}

setInterval(function(){
randomColor();

},1000);
</script>
</head>
<body>
<a href = "javascript:;">向左走</a>
<a href = "javascript:;">向右走</a>
<!--<div id="div">
<ul>
<li><img src="img/1.jpg" /></li>
<li><img src="img/2.jpg" /></li>
<li><img src="img/3.jpg" /></li>
<li><img src="img/4.jpg" /></li>
</ul>
-->
</div>
<div id="col" style="width:1000px;height:1000px;background:#FFF;margin:0 0 0 100px;"></div>
</body>
</html>

时间: 2024-08-25 20:14:23

js学习 无缝滚动 和 随机色的相关文章

JS 实现无缝滚动动画原理(初学者入)

这段时间在教培训班的学生使用原生javascript实现无缝滚动的动画案例,做了这个原理演示的动画,分享给自学JS的朋友!博主希望对你们有帮助! 在讲解之前先看一下demo: demo:https://224137748.github.io/JS_warehouse/lunbo/domo.HTML源码:https://github.com/224137748/JS_warehouse/blob/master/lunbo/domo.HTML ps: 上面和下面的滚动进度是一致的,上面红色框是为了演

js图片无缝滚动代码

想必大家都注意到<marquee>的不循环滚动,所以出现了很多替代脚本,或iframe或JS输出<marquee>,不管怎么做,都略显麻烦.下面说一下这个相对简单的实现思路:一个设定宽度并且隐藏超出它宽度的内容的容器demo,里面放demo1和 demo2,demo1是滚动内容,demo2为demo1的直接克隆,通过不断改变demo1的scrollTop或者scrollLeft达到滚动的目的,当滚动至demo1与demo2的交界处时直接跳回初始位置,因为demo1与demo2一样,

JS平滑无缝滚动实现

本文我们实现纯JS方式的滚动广告效果. 先show一下成品: 首先是网页样式: 1. #demo { 2. background: #FFF; 3. overflow:hidden; 4. border: 1px dashed #CCC; 5. width: 1280px; 6. height:200px; 7. } 8. #demo img { 9. border: 3px solid #F2F2F2; 10. } 11. #indemo { 12. float: left; 13. widt

js图片无缝滚动

<html> <head> <title>图片滚动</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <style> #demo { background:#fff; overflow: hidden; width:500px; border: 1px solid #ccc; } #demo

CSS/JS实现无缝滚动字幕

实现思路 定义两个滚动块,内容一模一样,从下往上两个紧挨着滚动,当第一个滚动块滚动出容器时(即第二个滚动块顶到了容器的上边缘),此时我们将第一个滚动块设置位置到容器上边缘,由于两个滚动块一模一样,从效果上我们也看不出来容器的内容发生了替换. html 代码 <div class="box" id="box"> <ul class="col1" id="col1"> <li>1</li&

js之无缝滚动

<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <style type="text/css"> *{margin:0px;padding:0xp;}//默认全局外边距0,内边距0 #div1 {width:800px;height:200px

js 图片无缝循环

<html> <head> <title>Js图片无缝滚动</title> <style type="text/css"> body{ background:gray; } #wrap{ width:810px; height:200px; border:1px solid black; position:relative; left:50%; top:50%; margin-left:-410px; margin-top:-

javascript如何设置DIV背景色为随机色

随机色有两种格式: 效果预览:http://wjf444128852.github.io/DEMOLIST/JS/test/index.html 1.rgb(xxx,xxx,xxx) 2.#xxxxxx 下面实现两种随机的方法 思路: 就是如何让x都是随机的, 1.中的xxx是0-255之间的随机整数,用Math.random()*255取得0-255之间的随机数, 再Math.floor()保留小数点前面的 2.中的x是0123456789abxdef中的随机6个的组合, 这里可以用数组或者字

【JS学习】无缝滚动

一.无缝滚动--基础 效果演示 物体运动基础 让Div移动起来 offsetLeft的作用,可以想到有offsetLeft就会有offsetRight,还会有offsetWight/offsetHeight 用定时器让物体连续移动 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&q