鼠标悬停时突出显示单行

时间:2013-03-01 00:11:23

标签: javascript jquery html css

当鼠标悬停在该行上时,我想更改多行段落中单行的背景颜色(覆盖该行中的任何单词) - 可以使用JQuery / JS实现吗?

若然,怎么样?

修改 为了澄清,我想在鼠标悬停后突出显示任何一行 该脚本必须动态隔离光标所在的行,并在鼠标悬停时对其应用临时样式。

编辑2:
插图的图片 -
http://i46.tinypic.com/nvv9y.png

4 个答案:

答案 0 :(得分:3)

这是一场艰苦的战斗,但我提出了一种方法来做到这一点,根本不需要容器上的样式(包括它的字体,对齐等)。

这不是一个完美的解决方案,但希望它适用于您的目的。

var
    //Keeps the content (individual spans) as they are built.
    $keeper = $("<div>"),
    //Used to measure span width for comparison to container.
    $measurer = $("<div>"),
    //The container of the text content
    $p = $("p"),
    //An individual line of the content
    $line = $("<span>").appendTo($measurer),

//make this "invisible," but allow for measurement against container width
//with no restriction on measurer's own width (fixed)
$measurer.css({'position': 'fixed', 'top': '100%'}).appendTo("body");

//Iterate through each "word" derived from splitting on any space.
$p.text().split(/\s/).forEach(function (elem) {
    //Start forming line text.  Don't forget word spacing
    $line.text($line.text() + elem + ' ');

    //Enough words to make the line width longer than the p width.
    //This indicates the end of "line."
    if ($line.width() > $p.width()) {
        //Remove the last word.
        $line.text(function (_, text) {
            return text.slice(0, text.lastIndexOf(elem));
        });

        //Keep the currently formed line to add back later
        $line.appendTo($keeper);

        //Create a new line for measuring
        $line = $("<span>");
        $line.text(' ' + elem).appendTo($measurer);
    }
});
//Append any leftover words not big enough to form a whole line
$keeper.append($measurer.html());
//Add back content
$p.html($keeper.html());
//cleanup
$keeper.remove();
$measurer.remove();

http://jsfiddle.net/6Cx3h/2/

如果容器的宽度取决于窗口,您也可以在窗口调整大小时执行此操作。

(你可以看到我尝试使用高度而不是http://jsfiddle.net/6Cx3h处的宽度)

答案 1 :(得分:1)

每一行应该是一个标记,如果需要,可以使用<span>,然后使用css(比javascript或jQuery更好)

span:hover{
  background-color: #ccc;
}

答案 2 :(得分:1)

这完全取决于您的文本最初的格式。从您显示的屏幕截图中,它看起来像文本被包裹在强制换行的元素中。在这种情况下,整个文本中没有真正的“新行”。如果元素的大小发生变化,它会改变......例如,你正在阅读的文本被包裹在网站的约束内......

但如果我要 插入我自己的行
休息,然后是 以下方法
可能有用。

// extract the text
var paragraph = $("#text").text();
// split the text into lines by searching for linebreaks
var lines = paragraph.split(/\r?\n/);
// clear the original text
$("#text").text('');
$.each(lines,function(index,elem){
  if (elem != ''){
    // for each line, wrap it with a span and bring back the linebreak
    $("#text").append('<span>'+elem+'</span><br/>');      
  }
});

现在你所要做的就是制定一些css规则来突出悬停事件中的span元素 -

span:hover { background:#DDD }

A live example

答案 3 :(得分:0)

您可以通过创建将放置在每行文本后面的块来模拟非格式化段落中单行的突出显示。此块的大小与p line-height相同,根据鼠标y位置,top位置可能会发生变化。此方法不能被视为原生突出显示,但它比其他建议有一些优势。首先,它可以适应任何文本。其次,即使在调整流体文本块的大小后,它也可以计算行数。第三,保持文本清晰,不受任何额外标记(<br/>)或其他breaks的影响。

HTML:

<p>Some long non-formatted - fluid text</p>

CSS:

p {
    position:relative;
    text-align:justify;
    font: 14px/18px Arial; /*Line-height is essential to be defined because not all browsers translate the default value in px - e.g. Chrome returns "normal" and FF returns pixels*/
}
p span {  /*The highlighter*/
    background:yellow; /*Highlight color*/
    display:none; /*Hide it until we have a hover*/
    position:absolute;
    left:0;
    z-index:-1; /*Place it behind the text*/
}

jQuery:

//get the line-height
var theLineheight = $('p').css('line-height'); 
//strip it from the 'px'
var Lineheight = parseInt(theLineheight); 
//get the text height
var thePheight = $('p').height(); 
//update the height after resize
window.onresize = function () {
    return thePheight = $('p').height(); 
}
//create the highlight box
$('p').append('<span style="height:' + theLineheight + ';width:100%"></span>'); 
//detect mouse movement in the text container
$('p').mousemove(function (e) {
    //show the highlighter
    $('p span').show();
    //get the mouse position
    mouseTop = e.pageY - this.offsetTop;
    //round it to a line-height scale
    topPos = Math.ceil(mouseTop / Lineheight) * Lineheight - Lineheight;
    //position the highlighter vertical
    $('p span').css('top', topPos + 'px');
    //hide the highlighter when mouse is at the end of the text - including the space that highlighter takes
    if (topPos > (thePheight - Lineheight)) {
        $('p span').hide();
    }
//hide the highlighter when mouse is not over the text
}).mouseout(function (e) {
    $('p span').hide();
});

以下是演示:http://jsfiddle.net/gh8gZ/1/

我可以看到我的建议的唯一缺点是,当你有一个空行的文本块时,它们也会突出显示,并且突出显示的区域占据整行的宽度 - 虽然这不会打扰我但我必须表明这不是一个完美的解决方案。

相关问题