0

JSFIDDLE

我需要做的是在某些文本范围内放置一些注释。这是我的代码

HTML

<!DOCTYPE html>
<html> 
<head>  

    <link rel="stylesheet" href="css/demo.css" />

    <script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="js/demo.js"></script>
</head>
<body>
    <div class="dialog-container">
        <div class="annotation-head"></div>
        <div class="annotation-segment">
            <span class="marker" data-anno-id="0">This is a</span> 
            <span class="marker" data-anno-id="1">sample</span> 
            text
            <span class="marker" data-anno-id="2">another one</span> 
            text
            <span class="marker" data-anno-id="3">one mooooorreee</span> 
        </div>
    </div>  
</body>
</html>

Javascript

String.prototype.width = function(font) {

  var f = font || '12px arial',
      o = $('<div>' + this + '</div>')
            .css({'position': 'absolute', 'float': 'left', 'white-space': 'nowrap', 'visibility': 'hidden', 'font': f})
            .appendTo($('body')),
      w = o.width();

  o.remove();

  return w;
}

$(document).ready(function() {

    var annos = ['first', 'second', 'third', 'forth']; // your annotation data

    $('.marker').each(function () {
        var $t = $(this),
            pos = parseInt($t.attr('data-anno-id')),
            annoStr = annos[pos]; 

        // create an annotation for each marker
        var top = this.offsetTop - 5, 
            left = this.offsetLeft + ($t.width() - annoStr.width())/2,
            width = $t.width(),
            style = 'style="top:' + top + 'px; left:' + left + 'px;"';

        $('.annotation-head').append('<span class="anno label" ' + style + '>' + annoStr + '</span>');
    });
});

CSS

.dialog-container {
    width: 600px;
    height: 200px;
    border-style: solid;
    border-width: 1px;
    border-color: black;
    border-radius: 10px;
    padding-left: 5px;
}

.annotation-head {
    padding-top: 7px;
}

.annotation-segment, .annotation-head {
    position: relative;
}

.marker {
    background: lightgreen;
}

.anno {
    position: relative;
    text-align: center;
}

.label {
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    border-radius: 3px;

    position: relative;
    display: inline-block;
    padding: 2px;
    font-size: 11.844px;
    font-weight: bold;
    line-height: 14px;
    color: #ffffff;
    text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25);
    white-space: nowrap;
    vertical-align: baseline;
    background-color: #999999;
}

我想我快到了。问题是,因为我对注释使用了不同的 div,而对实际文本使用了不同的 div,所以我无法将注释放在正确的跨度之上。它适用于第一个注释(将标签放在下面文本的中间),但之后就毁了。我需要不同的 div(这是更大程序的一部分),我不能使用

位置:绝对

有没有办法计算标签的右左偏移量?

4

1 回答 1

1

尝试这个

http://jsfiddle.net/jPYgM/6/

.dialog-container {
    width: 600px;
    height: 200px;
    border-style: solid;
    border-width: 1px;
    border-color: black;
    border-radius: 10px;
    padding-left: 5px;
}

.annotation-head {
    padding-top: 7px;
}

.annotation-segment, .annotation-head {
    position: relative;
}

.marker {
    background: lightgreen;
}

.anno {
    position: relative;
    text-align: center;

}

.label {
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    border-radius: 3px;

    display: inline-block;
    padding: 2px 0 ;
    font-size: 11.844px;
    font-weight: bold;
    line-height: 14px;
    color: #ffffff;
    text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25);
    white-space: nowrap;
    vertical-align: baseline;
    background-color: #999999;
}

    String.prototype.width = function(font) {

  var f = font || '12px arial',
      o = $('<div>' + this + '</div>')
            .css({'position': 'absolute', 'float': 'left', 'white-space': 'nowrap', 'visibility': 'hidden', 'font': f})
            .appendTo($('body')),
      w = o.width();

  o.remove();

  return w;
}

$(document).ready(function() {

    var annos = ['first', 'second', 'third', 'forth']; // your annotation data

    $('.marker').each(function () {
        var $t = $(this),
            pos = parseInt($t.attr('data-anno-id')),
            annoStr = annos[pos]; 
        var total_width=0;
        $('.annotation-head .anno').each(function(){
        total_width += $(this).width();
        })
        // create an annotation for each marker
        var top = this.offsetTop - 5, 
            left = this.offsetLeft -total_width,
            width = $t.width(),
                style = 'style="top:' + top + 'px; left:' + left + 'px;width:'+width +'px;"';

        $('.annotation-head').append('<span class="anno label" ' + style + '>' + annoStr + '</span>');
    });
});
于 2013-06-24T12:02:48.147 回答