gpt4 book ai didi

javascript - 当目标元素不在中心时如何更改弹出箭头?

转载 作者:太空宇宙 更新时间:2023-11-04 09:13:49 25 4
gpt4 key购买 nike

我有以下问题。当 Tether 移动 Popover 使其保持在视口(viewport)内时,其箭头仍位于中心。在下图中,弹出窗口附加到第一张图片(鼠标光标所在的位置),但它看起来好像附加到第二张图片,因为小箭头仍在中心。有办法解决这个问题吗?

enter image description here

这是我的代码

$('.popover-image').popover({
html: true,
trigger: 'hover',
placement: 'bottom',
constraints: [{
to: 'scrollParent',
attachment: 'together',
pin: true
}],
title: $(this).data('title'),
content: function(){return '<img src="'+$(this).attr('src') + '" />';}
});

最佳答案

Screen Shot

图像上的弹出框显示弹出框

$(document).ready(function(){
//$('[data-toggle="popover"]').popover();
$('.popover-image').popover({
html: true,
trigger: 'hover',
placement: 'bottom',
constraints: [{
to: 'scrollParent',
attachment: 'together',
pin: true
}],
title: $(this).data('title'),
container: 'body',
content: function(){return '<img src="'+$(this).attr('src') + '" class="img-responsive img-center" />';}
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
.popover{
max-width: 50%; /* Max Width of the popover (depending on the container!) */
position: relative;
}
.img-center {margin:0 auto;}
</style>
</head>
<body>

<div class="container">
<div class="row">
<div class = "col-sm-4">
<div class = "caption">
<p>Some sample text. Some sample text.</p>
</div>
<div class = "thumbnail">
<img src="http://placehold.it/350x150" class="popover-image" data-trigger="hover" title="test image">
</div>
</div>
<div class = "col-sm-4">
<div class = "caption">
<p>Some sample text. Some sample text.</p>
</div>
<div class = "thumbnail">
<img src="http://placehold.it/350x150" class="popover-image" data-trigger="hover" title="test image">
</div>
</div>
<div class = "col-sm-4">
<div class = "caption">
<p>Some sample text. Some sample text.</p>
</div>
<div class = "thumbnail">
<img src="http://placehold.it/350x150" class="popover-image" data-trigger="hover" title="test image">
</div>
</div>
</div>

<div class="row">
<div class = "col-sm-4">
<div class = "caption">
<p>Some sample text. Some sample text.</p>
</div>
<div class = "thumbnail">
<img src="http://placehold.it/350x150" class="popover-image" data-trigger="hover" title="test image">
</div>
</div>
<div class = "col-sm-4">
<div class = "caption">
<p>Some sample text. Some sample text.</p>
</div>
<div class = "thumbnail">
<img src="http://placehold.it/350x150" class="popover-image" data-trigger="hover" title="test image">
</div>
</div>
<div class = "col-sm-4">
<div class = "caption">
<p>Some sample text. Some sample text.</p>
</div>
<div class = "thumbnail">
<img src="http://placehold.it/350x150" class="popover-image" data-trigger="hover" title="test image">
</div>
</div>
</div>

</div>
</body>
</html>

编辑使用 Bootstrap 4

enter image description here

$(document).ready(function(){
$('[data-toggle="popover"]').popover();
$('.popover-image').popover({
html: true,
trigger: 'hover',
placement: 'bottom',
constraints: [{
to: 'scrollParent',
attachment: 'together',
pin: true
}],
title: $(this).data('title'),
container: 'body',
content: function(){return '<img src="'+$(this).attr('src') + '" class="img-fluid" />';}
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/css/tether-theme-arrows-dark.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>

<style>
.popover{
max-width: 100%; /* Max Width of the popover (depending on the container!) */
}
.img-center {margin:0 auto;}
</style>
</head>
<body>

<div class="container">
<div class="row">
<div class = "col-4">
<div class="card">
<div class="card-block">
<p class="card-text">Some quick example .</p>
</div>
<img class="card-img-top popover-image" src="http://placehold.it/350x150" alt="Card image cap" title="test image">
</div>
</div>
<div class = "col-4">
<div class="card">
<div class="card-block">
<p class="card-text">Some quick example .</p>
</div>
<img class="card-img-top popover-image" src="http://placehold.it/350x150" alt="Card image cap" title="test image">
</div>
</div>
<div class = "col-4">
<div class="card">
<div class="card-block">
<p class="card-text">Some quick example .</p>
</div>
<img class="card-img-top popover-image" src="http://placehold.it/350x150" alt="Card image cap" title="test image">
</div>
</div>
</div>
<div class="row">
<div class = "col-4">
<div class="card">
<div class="card-block">
<p class="card-text">Some quick example .</p>
</div>
<img class="card-img-top popover-image" src="http://placehold.it/350x150" alt="Card image cap" title="test image">
</div>
</div>
<div class = "col-4">
<div class="card">
<div class="card-block">
<p class="card-text">Some quick example .</p>
</div>
<img class="card-img-top popover-image" src="http://placehold.it/350x150" alt="Card image cap" title="test image">
</div>
</div>
<div class = "col-4">
<div class="card">
<div class="card-block">
<p class="card-text">Some quick example .</p>
</div>
<img class="card-img-top popover-image" src="http://placehold.it/350x150" alt="Card image cap" title="test image">
</div>
</div>
</div>

</div>
</body>
</html>

关于javascript - 当目标元素不在中心时如何更改弹出箭头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41907166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com