[英]Marquee in HTML without <marquee> .. </marquee>
我正在尝试编写流畅运行的滚动文本。 <marquee>..</marquee>
标记如果没有摇晃就无法工作,而且我认为这不是很好的编程方法。 我想用JavaScript来做,但是我是一个初学者。
我找到了一些易于理解的代码,但是我认为看起来最好的滚动文本与我不一致。
也许有人可以向我解释我听不懂的部分。
码:
var marqueewidth="2400px"
var marqueeheight="45px"
var speed=1
var pause=1 //stop by mouseover 0=no. 1=yes
var marqueecontent='<nobr><span style="font-size:40px">*** Wir wünschen einen guten Start in den Dezember!!! ***</span></nobr>'
var newspeed=speed
var pausespeed=(pause==0)? newspeed: 0
document.write('<span id="temp" style="visibility:hidden; position:absolute; top:0px; left:-9000px">'+marqueecontent+'</span>')
var actualwidth=''
var cross_marquee, ns_marquee
function populate(){
cross_marquee= document.getElementById("marquee")
cross_marquee.style.left=parseInt(marqueewidth)+8+"px"
cross_marquee.innerHTML=marqueecontent
actualwidth=document.getElementById("temp").offsetWidth
lefttime=setInterval("scrollmarquee()",20)
}
window.onload=populate
function scrollmarquee(){
if (parseInt(cross_marquee.style.left)>(actualwidth*(-1)+8))
cross_marquee.style.left=parseInt(cross_marquee.style.left)-newspeed+"px"
else
cross_marquee.style.left=parseInt(marqueewidth)+8+"px"
}
with (document){
write('<div style="position:relative; top:655px; width:'+marqueewidth+'; height:'+marqueeheight+'; overflow:hidden">')
write('<div onMouseover="newspeed=pausespeed" onMouseout="newspeed=speed">')
write('<div id="marquee" style="position:absolute; left:0px; top:0px; "></div>')
write('</div></div>')
}
问题:为什么我需要temp div? 以及如何在CSS中交换样式?
我只是会使用CSS3-动画。 它可以像魅力一样在每种现代浏览器中运行。 您不需要JavaScript即可移动元素。
如果要打开和关闭它,只需添加和删除CSS类。
我刚刚在Codepen中构建了一个示例: http ://codepen.io/anon/pen/LGEBbx
这是动画代码:
@keyframes marquee {
0% { transform: translateX(100%); }
100% { transform: translateX(-100%); }
}
我希望这是您正在寻找的解决方案。
好吧, marquee
不仅被弃用了,现在也已经过时了。
当然,您可以创建一个模拟效果的JavaScript函数。 但是使用CSS这样做更简单,当然也更流畅。
这是一个例子:
的HTML
<div class="wrapper">
<p>Hey, how you're doing? Sorry you can't get through.</p>
</div>
的CSS
.wrapper {
position: relative;
overflow: hidden;
height: 25px;
width: 100px;
border: 1px solid orange;
}
.wrapper p {
position: absolute;
margin: 0;
line-height: 25px;
white-space: nowrap;
animation: marquee 5s linear infinite;
}
@keyframes marquee {
0% { transform: translateX(100%); }
100% { transform: translateX(-100%); }
}
演示版
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.