【问题标题】:highlight multi line sequential (with jquery)突出显示多行顺序(使用 jquery)
【发布时间】:2012-08-25 15:43:46
【问题描述】:

我使用此代码在特定时间突出显示我的 span 标签

HTML:

<span id="test">The word "fractal" often has different 
connotations for laypeople than mathematicians, where the
layperson is more likely to be familiar with fractal art
than a mathematical conception. The mathematical concept 
is difficult to formally define even for mathematicians,
but key features can be understood with little mathematical background.</span>

JavaScript:

$(document).ready(function(){

  var seconds = 7;
  var el = $('span#test');
  var width = el.outerWidth();
  var height = el.outerHeight();
  var wrapper = $('<div>').css({
    width: width + 'px',
    height: height + 'px',
    position: 'relative'
  });
  var background = $('<div>').css({
    width: 0,
    height: height + 'px',
    position: 'absolute',
    background: '#0f0'
  });
  wrapper.append(background);
  wrapper.append(el.css('position','absolute'));
  $('body').append(wrapper);
  background.animate({width: '+=' + width},1000*seconds);

});​​

演示: http://jsfiddle.net/9UgEF/8/

一切都很好,但我想突出显示span tagsequential(它突出显示所有行,我想突出显示连续的行(当第一行突出显示完成后转到下一行......))。 我该怎么做?

【问题讨论】:

标签: jquery


【解决方案1】:
$(function(){
    var time = 30,
        text =  $("#test").text(),
        words = text.split(''),
        html = [];
    $.each(words, function(i,e) {
        html.push('<span>'+ e +'</span>');
    });
    $("#test").html(html.join(''));
    $("span", "#test").each(function(i,e) {
        $(e).delay(i*((30*1000)/$("span", "#test").length)).animate({'background-color': '#0f0'}, 500);
    });
});​

FIDDLE

【讨论】:

  • 不客气!请注意,颜色动画使用的是 jQuery UI,如果您不将 UI 库用于其他内容,则有一个小得多的插件仅用于动画颜色,您甚至可以在没有动画的情况下执行此操作,只是不会那样光滑。
  • 如何控制花费准确的时间?(例如,我想在 30 秒内突出显示所有行)
  • @irsog - 为此目的更新了答案以包含 time 变量,请参阅答案中的更新小提琴?
  • -@adeneo 对不起,我是 jquery 的新手,另一个问题:我该怎么做:jsfiddle.net/9UgEF/37
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-03
  • 1970-01-01
  • 2016-06-11
  • 2010-10-20
  • 2011-11-22
  • 1970-01-01
相关资源
最近更新 更多