没有<marquee> .. </marquee>的HTML选框

时间:2015-12-08 15:03:52

标签: javascript html 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>')
}

问题:为什么我需要临时div?我怎样才能在CSS中交换样式?

2 个答案:

答案 0 :(得分:4)

我只会使用CSS3动画。它适用于每个现代浏览器,如魅力。您不需要JavaScript来移动元素。

如果要打开和关闭它,只需添加和删除CSS类。

我刚在codepen中构建了一个示例:http://codepen.io/anon/pen/LGEBbx

这是动画代码:

@keyframes marquee {
  0% { transform: translateX(100%); }
  100% { transform: translateX(-100%); }
}

我希望这是你正在寻找的解决方案。

答案 1 :(得分:3)

好吧,marquee不仅被弃用,现在也是obsolete

当然,您可以创建一个模拟效果的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%); }
}

<强>演示

Try before buy

相关问题