Почему Twitter не загружается с помощью SVG-элементов?
Я больше парень из С++, но сейчас я работаю над некоторыми материалами веб-интерфейса. Кажется, я не могу получить всплывающее окно Bootstrap для отображения на элементе SVG.
Любые идеи? Попсор работает с обычным div.
jsFiddle здесь: http://jsfiddle.net/JjAht/
<!DOCTYPE html>
<html>
<head>
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.2.2/css/bootstrap.min.css" rel="stylesheet">
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.2.2/css/bootstrap-responsive.min.css" rel="stylesheet">
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.2.2/bootstrap.min.js"></script>
</head>
<body>
<div>
<svg width="100" height="100">
<circle r="45" cx="50" cy="50" style="fill: red"/>
</svg>
</div>
<div class="well">
<p>
Hover here for a popover.
</p>
</div>
<script type="text/javascript">
$(document).ready(function() {
var numCircles = $("circle").length;
console.log("numCircles = " + numCircles);
$("circle").popover({trigger:'hover', placement:'bottom', title:'Title!', content:'Content'});
$("circle").css({"stroke":"blue"});
$(".well").popover({trigger:'hover', placement:'bottom', title:'Title!', content:'Content'});
} );
</script>
</body>
</html>
Ответы
Ответ 1
В этом примере, похоже, вам нужно изменить Bootstrap, чтобы заставить это работать.
В bootstrap-tooltip.js заменить:
$tip
.detach()
.css({ top: 0, left: 0, display: 'block' })
.insertAfter(this.$element)
pos = this.getPosition(inside)
с
if($('#'+this.$element[0].id,$('svg')).length > 0){
$tip
.remove()
.css({ top: 0, left: 0, display: 'block' })
.prependTo(document.body)
pos = $.extend({}, this.$element.offset(), {
width: this.$element[0].getBoundingClientRect().width
, height: this.$element[0].getBoundingClientRect().height
})
} else {
$tip
.detach()
.css({ top: 0, left: 0, display: 'block' })
.insertAfter(this.$element)
pos = this.getPosition(inside)
}
В основном было две проблемы: 1) bootstrap вставлял div для всплывающей подсказки /popover в SVG, где браузер игнорирует его, и 2) информация о местоположении должна быть рассчитана из SVG
Я представил это как запрос на перенос на Github https://github.com/twitter/bootstrap/pull/6521
Обновление: Похоже, что bootstrap 2.3.0+ будет поддерживать это через новое свойство container
для всплывающих подсказок /popovers. При работе с SVG установите container
в body
. См. Комментарии здесь https://github.com/twitter/bootstrap/pull/6521
Пример:
svg.selectAll(".node").each(
function(d,i){
$(this).popover({title:"title", content:"content", container:"body"});
// and/or $(this).tooltip({title:"tooltip - title", container:"body"});
});
Ответ 2
С последней версией Bootstrap добавление опции container
с элементом HTML внутри кажется достаточным, чтобы заставить его работать!
$("circle").each(function () {
$(this).popover({
title: "Hi!",
content: "popover",
container: $("#container")
});
});
Ответ 3
вы не выбрали svg, чтобы открыть popover, поэтому он не откроется. Посмотрите здесь http://jsfiddle.net/JjAht/1/
$("svg").popover({trigger:'hover', placement:'bottom', title:'Title!', content:'Content'});