为什么不推荐使用 < marquee > ,最好的替代方案是什么?

我对 HTML 标记 <marquee>感到好奇的时间更长了。

你可在 MDN 规范找到:

这个特性已经过时了。虽然它可能仍然在某些浏览器中工作,但它的使用是不鼓励的,因为它可能在任何时候被删除。尽量避免使用它。

或在 W3C 维基频道播放:

不,真的,别用它。

我搜索了几篇文章,发现了一些与 CSS 相关的替代品。 CSS 属性如下:

marquee-play-count
marquee-direction
marquee-speed

但看起来,它们不起作用。它们在 二零零八年年是规范的一部分,但在 二零一四年年被排除在外

W3 Consortium 提出的一种方法是使用 CSS3动画,但对我来说,它似乎比易于维护的 <marquee>复杂得多。

还有大量的 JS 替代品,以及大量的源代码,您可以将它们添加到您的项目中,并使它们变得更大。

我总是读到这样的东西: “永远不要使用选框”,“已经过时了”。我不明白为什么。

那么,谁能给我解释一下,为什么是不推荐的,为什么使用它和 最简单的替代品是什么会这么“危险”呢?

我找到了一个 例子看起来不错。当您使用好的浏览器支持所需的所有前缀时,您有大约20-25行 CSS,其中2个值是硬编码的(开始和停止缩进) ,具体取决于文本长度。这个解决方案没有那么灵活,而且您不能用它创建自底向上的效果。

77139 次浏览

I don't think you should move the content but that doesn't answer your question... Take a look at the CSS:

.marquee {
width: 450px;
line-height: 50px;
background-color: red;
color: white;
white-space: nowrap;
overflow: hidden;
box-sizing: border-box;
}
.marquee p {
display: inline-block;
padding-left: 100%;
animation: marquee 15s linear infinite;
}
@keyframes marquee {
0%   { transform: translate(0, 0); }
100% { transform: translate(-100%, 0); }
}

Here is the codepen.

Edit:

Here is the bottom to top codepen.

You just have to define class and attached looping animation once in CSS and use it afterwards everywhere you need. But, as many people said - it's a bit annoying practice, and there is a good reason, why this tag is becoming obsolete.

.example1 {
height: 50px;
overflow: hidden;
position: relative;
}
.example1 h3 {
position: absolute;
width: 100%;
height: 100%;
margin: 0;
line-height: 50px;
text-align: center;


/* Starting position */
-moz-transform:translateX(100%);
-webkit-transform:translateX(100%);
transform:translateX(100%);


/* Apply animation to this element */
-moz-animation: example1 15s linear infinite;
-webkit-animation: example1 15s linear infinite;
animation: example1 15s linear infinite;
}


/* Move it (define the animation) */
@-moz-keyframes example1 {
0%   { -moz-transform: translateX(100%); }
100% { -moz-transform: translateX(-100%); }
}
@-webkit-keyframes example1 {
0%   { -webkit-transform: translateX(100%); }
100% { -webkit-transform: translateX(-100%); }
}
@keyframes example1 {
0%   {
-moz-transform: translateX(100%); /* Firefox bug fix */
-webkit-transform: translateX(100%); /* Firefox bug fix */
transform: translateX(100%);
}
100% {
-moz-transform: translateX(-100%); /* Firefox bug fix */
-webkit-transform: translateX(-100%); /* Firefox bug fix */
transform: translateX(-100%);
}
}
<div class="example1">
<h3>Scrolling text... </h3>
</div>

As stated before: the easiest substitution is CSS animation

To all the critics of the marquee:

It is a very useful tool for UI, I am using it just on hover, to display more information in a limited space.

The example for the mp3-player is excellent, even my car-radio is using the effect to show the current song.

So nothing wrong about that, my opinion ...

<marquee> was never part of any HTML specification and what you link to is a CSS spec so it's hard to deprecate something that was never included. HTML is about structure of a document, not its presentation. So having a self-animated element as part of HTML does not abide by those goals. Animation is in CSS.

I know this was answered a couple years ago, but I found this when inspecting this. When I inspected, I found this.

@keyframes scroll {
from {
transform: translate(0,0)
}


to {
transform: translate(-300px,0)
}
}


.resultMarquee {
animation: scroll 7s linear 0s infinite;
position: absolute
}

I have created a jQuery script that will replace the old marquee tag with standard div. The code will also parse the marquee attributes like direction, scrolldelay and scrollamount. Actually the code can skip the jQuery part but I felt too lazy to do so, and the vanilla JS part is actually a solution that I modified from @Stano answere from here

Here is the code:

jQuery(function ($) {


if ($('marquee').length == 0) {
return;
}


$('marquee').each(function () {


let direction = $(this).attr('direction');
let scrollamount = $(this).attr('scrollamount');
let scrolldelay = $(this).attr('scrolldelay');


let newMarquee = $('<div class="new-marquee"></div>');
$(newMarquee).html($(this).html());
$(newMarquee).attr('direction',direction);
$(newMarquee).attr('scrollamount',scrollamount);
$(newMarquee).attr('scrolldelay',scrolldelay);
$(newMarquee).css('white-space', 'nowrap');


let wrapper = $('<div style="overflow:hidden"></div>').append(newMarquee);
$(this).replaceWith(wrapper);


});


function start_marquee() {


let marqueeElements = document.getElementsByClassName('new-marquee');
let marqueLen = marqueeElements.length
for (let k = 0; k < marqueLen; k++) {




let space = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
let marqueeEl = marqueeElements[k];


let direction = marqueeEl.getAttribute('direction');
let scrolldelay = marqueeEl.getAttribute('scrolldelay') * 100;
let scrollamount = marqueeEl.getAttribute('scrollamount');


let marqueeText = marqueeEl.innerHTML;


marqueeEl.innerHTML = marqueeText + space;
marqueeEl.style.position = 'absolute';


let width = (marqueeEl.clientWidth + 1);
let i = (direction == 'rigth') ? width : 0;
let step = (scrollamount !== undefined) ? parseInt(scrollamount) : 3;


marqueeEl.style.position = '';
marqueeEl.innerHTML = marqueeText + space + marqueeText + space;






let x = setInterval( function () {


if ( direction.toLowerCase() == 'left') {


i = i < width ? i + step : 1;
marqueeEl.style.marginLeft = -i + 'px';


} else {


i = i > -width ? i - step : width;
marqueeEl.style.marginLeft = -i + 'px';


}


}, scrolldelay);


}
}


start_marquee ();
});

And here is a working codepen