Javascript 新闻报价器-预到期重启
我正在设法让这台新闻播报机工作。出于某种原因,它将在第二个列表项之后重新启动,这不是我想要的-我希望它在列表项中旋转直到最后。这个脚本有什么问题 这里有一个 HTMLJavascript 新闻报价器-预到期重启,javascript,jquery,html,marquee,ticker,Javascript,Jquery,Html,Marquee,Ticker,我正在设法让这台新闻播报机工作。出于某种原因,它将在第二个列表项之后重新启动,这不是我想要的-我希望它在列表项中旋转直到最后。这个脚本有什么问题 这里有一个 HTML <h1>This runs differently than the ticker for some reason. It's highly annoying in my personal opinion.</h1> <div id="ticker"> <div class
<h1>This runs differently than the ticker for some reason. It's highly annoying in my personal opinion.</h1>
<div id="ticker">
<div class="event">test1</div>
<div class="event">test2</div>
<div class="event">test3</div>
<div class="event">test4</div>
</div>
Javascript/Jquery
(function($) {
$.fn.textWidth = function(){
var calc = '<span style="display:none">' + $(this).text() + '</span>';
$('body').append(calc);
var width = $('body').find('span:last').width();
$('body').find('span:last').remove();
return width;
};
$.fn.marquee = function(args) {
var that = $(this);
var textWidth = that.textWidth(),
offset = that.width(),
width = offset,
css = {
'text-indent' : that.css('text-indent'),
'overflow' : that.css('overflow'),
'white-space' : that.css('white-space')
},
marqueeCss = {
'text-indent' : width,
'overflow' : 'hidden',
'white-space' : 'nowrap'
},
args = $.extend(true, { count: -1, speed: 1e1, leftToRight: false }, args),
i = 0,
stop = textWidth*-1,
dfd = $.Deferred();
function go() {
if (that.data('isStopped') != 1)
{
if(!that.length) return dfd.reject();
if(width == stop) {
i++;
if(i == args.count) {
that.css(css);
return dfd.resolve();
}
if(args.leftToRight) {
width = textWidth*-1;
} else {
width = offset;
}
}
that.css('text-indent', width + 'px');
if(args.leftToRight) {
width++;
} else {
width--;
}
}
setTimeout(go, 10);
};
if(args.leftToRight) {
width = textWidth*-1;
width++;
stop = offset;
} else {
width--;
}
that.css(marqueeCss);
that.data('isStopped', 0);
that.bind('mouseover', function() { $(this).data('isStopped', 1); }).bind('mouseout', function() { $(this).data('isStopped', 0); });
go();
return dfd.promise();
};
})(jQuery);
$('h1').marquee();
$('#ticker').marquee();
(函数($){
$.fn.textWidth=函数(){
var calc=''+$(this).text()+'';
$('body')。追加(计算);
var width=$('body').find('span:last').width();
$('body').find('span:last').remove();
返回宽度;
};
$.fn.marquee=函数(args){
var,该值=$(此值);
var textWidth=that.textWidth(),
偏移量=该.width(),
宽度=偏移量,
css={
“文本缩进”:that.css('text-indent'),
“溢出”:that.css('overflow'),
“空白”:that.css('white-space')
},
marqueeCss={
“文本缩进”:宽度,
“溢出”:“隐藏”,
“空白”:“nowrap”
},
args=$.extend(true,{count:-1,速度:1e1,leftToRight:false},args),
i=0,
停止=文本宽度*-1,
dfd=$.Deferred();
函数go(){
如果(该.data('isStopped')!=1)
{
如果(!that.length)返回dfd.reject();
如果(宽度==停止){
i++;
if(i==args.count){
css(css);
返回dfd.resolve();
}
if(参数leftToRight){
宽度=文本宽度*-1;
}否则{
宽度=偏移量;
}
}
css('text-indent',width+'px');
if(参数leftToRight){
宽度++;
}否则{
宽度--;
}
}
设置超时(go,10);
};
if(参数leftToRight){
宽度=文本宽度*-1;
宽度++;
停止=偏移;
}否则{
宽度--;
}
css(marqueeCss);
数据('isStopped',0);
bind('mouseover',function(){$(this).data('isStopped',1);}).bind('mouseout',function(){$(this.data('isStopped',0);});
go();
返回dfd.promise();
};
})(jQuery);
$('h1').marquee();
$('ticker').marquee();
把它放在一个容器div中
<div id="ticker-container">
<h1>This runs differently than the ticker for some reason. It's highly annoying in my personal opinion.</h1> <div id="ticker">
<div class="event">test1</div>
<div class="event">test2</div>
<div class="event">test3</div>
<div class="event">test4</div>
</div>
</div>
$('#ticker-container').marquee();
由于某些原因,它的运行方式与股票代码不同。我个人认为这很烦人。
测试1
测试2
测试3
测试4
$(“#股票代码容器”).marquee();
但这并不能真正解决问题。股票代码将沿着h1的长度运行,但如果没有h1,它将在最后2秒后重新启动。h1只是一个示例,脚本的目的是用作新闻代码,因此test1-4将是事件。
<div id="ticker-container">
<h1>This runs differently than the ticker for some reason. It's highly annoying in my personal opinion.</h1> <div id="ticker">
<div class="event">test1</div>
<div class="event">test2</div>
<div class="event">test3</div>
<div class="event">test4</div>
</div>
</div>
$('#ticker-container').marquee();