简体   繁体   English

不带HTML的字幕 <marquee> .. </marquee>

[英]Marquee in HTML without <marquee> .. </marquee>

I'm trying to program a scrolling text that runs smooth. 我正在尝试编写流畅运行的滚动文本。 The <marquee>..</marquee> tag doesn't work without jolting and I don't think it is good programming. <marquee>..</marquee>标记如果没有摇晃就无法工作,而且我认为这不是很好的编程方法。 I would like to do it in JavaScript but I'm a total beginner in it. 我想用JavaScript来做,但是我是一个初学者。

I found some codes that are easy to understand but the scrolling text that I think looks best isn't coherent to me. 我找到了一些易于理解的代码,但是我认为看起来最好的滚动文本与我不一致。

Perhaps someone can explain the parts to me I don't understand. 也许有人可以向我解释我听不懂的部分。

CODE: 码:

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>')
}

Question: Why do I need the temp div ? 问题:为什么我需要temp div? And how can I swap the styles in CSS ? 以及如何在CSS中交换样式?

I just would use CSS3-animations. 我只是会使用CSS3-动画。 It works in every modern browser like a charm. 它可以像魅力一样在每种现代浏览器中运行。 You don't need JavaScript to move an element. 您不需要JavaScript即可移动元素。

If you want to switch it on and off, just add and remove a CSS class. 如果要打开和关闭它,只需添加和删除CSS类。

I just built an example in codepen: http://codepen.io/anon/pen/LGEBbx 我刚刚在Codepen中构建了一个示例: http ://codepen.io/anon/pen/LGEBbx

This is the animation code: 这是动画代码:

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

I hope this was the solution you're looking for. 我希望这是您正在寻找的解决方案。

Well, marquee isn't only deprecated, it's also obsolete now. 好吧, marquee不仅被弃用了,现在也已经过时了。

Of course you can create a JavaScript function that simulates the effect. 当然,您可以创建一个模拟效果的JavaScript函数。 But it's simpler and certainly smoother to use CSS for that. 但是使用CSS这样做更简单,当然也更流畅。

Here's an example: 这是一个例子:

HTML 的HTML

<div class="wrapper">
    <p>Hey, how you're doing? Sorry you can't get through.</p>
</div>

CSS 的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%); }
}

Demo 演示版

Try before buy 购买前尝试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM