jquery - 在 div 包装内跨度

标签 jquery css html

我在 div 中有一个 span 元素,像这样

<div id='news_ticker'><span id='news_ticker_text'>this is some long string that is long</span></div>

当我使用 JQuery 的动画在文档中移动跨度时,跨度及其文本转到 div 中的下一行。我尝试将 div 宽度设置为非常大的宽度,但它起作用了,但是有更好的解决方案吗?

我的代码是这样的

 #news_ticker_text { 
   visibility: hidden;
}
#news_ticker {
    background-color:black;
    position:fixed;
    width: 9999%;
    overflow: hidden;
}


/** Slides ticker text right. */
function slide_ticker_text() {
    var width = $(document).width();
    reset_ticker_text();
    $("#news_ticker_text").animate({
    marginLeft: width + "px",
    }, 25000, 'linear' /* Progresses linearly instead of easing.*/,
    function() {
        setTimeout(slide_ticker_text, 50);
    });
}
/** Resets ticker text to starting position. */
function reset_ticker_text() {
    var onset = -1 * $('#news_ticker_text').width() +"px";
    $('#news_ticker_text').css({
        visibility: "visible",
        marginLeft: onset
    });
}
setTimeout(slide_ticker_text, 50);

最佳答案

使用 white-space跨度上的属性:

#news_ticker_text {
    white-space: nowrap;
}

这样,您的跨度文本将只使用您专门提供的换行符。

关于jquery - 在 div 包装内跨度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22755032/

相关文章:

javascript - jquery淡入淡出导航重复淡入淡出?

javascript - 如何在javascript中获取当前文件路径

jquery - 使用 Bootstrap 创建可在单击时展开的垂直下拉菜单

html - 使用 CSS 移动和旋转

javascript - CSS 媒体查询不足以检测超大型移动/平板设备与桌面设备

javascript - 对混合字符串和数值的列字段进行排序

html - 右侧 div 的放置和并排设计

html - 使用运行其他页面的 WordPress 创建纯 html 主页 AMP

HTML5,创建一个左,右和顶部中心的类别过滤栏?

javascript - 正则表达式使用javascript用英语和阿拉伯语字符串中的空格替换特殊字符