如何使用 jquery 或 css 在 bootstrap 中制作可拖动弹出窗口?
How to make draggable pop over in bootstrap using jquery or css?
我想要可拖动或可移动的弹出框,因为当在一个页面中打开两个弹出框时,两个弹出框会相互重叠。对于这件事,你可以查看下面的截图。
所以我想使用鼠标指针将弹出框移动到我想要放置的位置。所以我可以同时看到两个弹出窗口。
我已经有代码了。
<!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.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h3>Popover Example</h3>
<a href="#" data-toggle="popover" title="Popover Header" data-content="Some content inside the popover">Toggle popover1</a>
<br/>
<a href="#" data-toggle="popover" title="Popover Header" data-content="Some content inside the popover">Toggle popover2</a>
</div>
<script>
$(document).ready(function(){
$('[data-toggle="popover"]').popover();
});
</script>
</body>
</html>
我想我可以使用可拖动功能,但它不起作用我不知道如何在 jQuery 上应用,所以它可以工作。
根据我的需要,你可以检查这个可移动的模态,我需要在我的弹出窗口上进行同样的可移动操作。为此,请查看下面的 gif 屏幕截图和 link 以获得更多理解。
Link: https://codepen.io/adamcjoiner/pen/PNbbbv
提前致谢。
建议:
尝试控制弹出窗口并覆盖 CSS.
$('#elm').popover({
placement: 'bottom',
html: true,
content: function() {
return $('#container').html();
},
template: '<div class="popover my-popover" role="tooltip"><div class="arrow"></div><div class="popover-content"></div></div>'
});
CSS样本:
.popover.my-popover {
margin-left: 15px;
margin-top: -20px;
/* Or: margin: -20px 0 0 15px; */
}
正在订阅 shown.bs.popover 事件:
$('#my-popover').on('shown.bs.popover', function() {
// parseInt removes "px"
var currentTop = parseInt($(this).css('top'));
var currentLeft = parseInt($(this).css('left'));
$(this).css({
top: (currentTop + 200) + 'px',
left: (currentLeft + 150) + 'px'
});
});
您可以在 "a" 标记中使用 data-placement="..." 来处理弹出窗口应该打开的位置。
例如,在您的代码中,它可以像:
<!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.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h3>Popover Example</h3>
<p>Popovers are not CSS-only plugins, and must therefore be initialized with jQuery: select the specified element and call the popover() method.</p>
<a href="#" data-toggle="popover" title="Popover Header" data-content="Some content inside the popover" data-placement="top">First popover</a>
<a href="#" data-toggle="popover" title="Popover Header" data-content="Some content inside the popover" data-placement="bottom">Second popover</a>
</div>
<script>
$(document).ready(function(){
$('[data-toggle="popover"]').popover();
});
</script>
</body>
</html>
data-placement 中的值可以是:top-bottom-right 和 left
更新:
我认为它对你有用:
$(".draggable").popover({
html:true,
content: "<p>Drag me</p>"
}).popover('show');
$('.popover').draggable();
.popover{position: relative !important}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.11.3/jquery-ui.js"
integrity="sha256-0vBSIAi/8FxkNOSKyPEfdGQzFDak1dlqFKBYqBp1yC4="
crossorigin="anonymous"></script>
<div class="row">
<div class ="col-auto">
<h2>
sendhelp
</h2>
</div>
</div>
<div id="Normal">
<a class="btn btn-primary draggable">Popover</a>
</div>
更新 2:
我做了一个 fiddle 你可以使用它:
https://jsfiddle.net/uhwrev06/
现在它完全随鼠标移动,它们之间没有 space!
更新 3:
我解决了你刚才提到的问题!
https://jsfiddle.net/pe2zf1x8/
我想要可拖动或可移动的弹出框,因为当在一个页面中打开两个弹出框时,两个弹出框会相互重叠。对于这件事,你可以查看下面的截图。
所以我想使用鼠标指针将弹出框移动到我想要放置的位置。所以我可以同时看到两个弹出窗口。
我已经有代码了。
<!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.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h3>Popover Example</h3>
<a href="#" data-toggle="popover" title="Popover Header" data-content="Some content inside the popover">Toggle popover1</a>
<br/>
<a href="#" data-toggle="popover" title="Popover Header" data-content="Some content inside the popover">Toggle popover2</a>
</div>
<script>
$(document).ready(function(){
$('[data-toggle="popover"]').popover();
});
</script>
</body>
</html>
我想我可以使用可拖动功能,但它不起作用我不知道如何在 jQuery 上应用,所以它可以工作。
根据我的需要,你可以检查这个可移动的模态,我需要在我的弹出窗口上进行同样的可移动操作。为此,请查看下面的 gif 屏幕截图和 link 以获得更多理解。
Link: https://codepen.io/adamcjoiner/pen/PNbbbv
提前致谢。
建议: 尝试控制弹出窗口并覆盖 CSS.
$('#elm').popover({
placement: 'bottom',
html: true,
content: function() {
return $('#container').html();
},
template: '<div class="popover my-popover" role="tooltip"><div class="arrow"></div><div class="popover-content"></div></div>'
});
CSS样本:
.popover.my-popover {
margin-left: 15px;
margin-top: -20px;
/* Or: margin: -20px 0 0 15px; */
}
正在订阅 shown.bs.popover 事件:
$('#my-popover').on('shown.bs.popover', function() {
// parseInt removes "px"
var currentTop = parseInt($(this).css('top'));
var currentLeft = parseInt($(this).css('left'));
$(this).css({
top: (currentTop + 200) + 'px',
left: (currentLeft + 150) + 'px'
});
});
您可以在 "a" 标记中使用 data-placement="..." 来处理弹出窗口应该打开的位置。 例如,在您的代码中,它可以像:
<!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.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h3>Popover Example</h3>
<p>Popovers are not CSS-only plugins, and must therefore be initialized with jQuery: select the specified element and call the popover() method.</p>
<a href="#" data-toggle="popover" title="Popover Header" data-content="Some content inside the popover" data-placement="top">First popover</a>
<a href="#" data-toggle="popover" title="Popover Header" data-content="Some content inside the popover" data-placement="bottom">Second popover</a>
</div>
<script>
$(document).ready(function(){
$('[data-toggle="popover"]').popover();
});
</script>
</body>
</html>
data-placement 中的值可以是:top-bottom-right 和 left
更新: 我认为它对你有用:
$(".draggable").popover({
html:true,
content: "<p>Drag me</p>"
}).popover('show');
$('.popover').draggable();
.popover{position: relative !important}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.11.3/jquery-ui.js"
integrity="sha256-0vBSIAi/8FxkNOSKyPEfdGQzFDak1dlqFKBYqBp1yC4="
crossorigin="anonymous"></script>
<div class="row">
<div class ="col-auto">
<h2>
sendhelp
</h2>
</div>
</div>
<div id="Normal">
<a class="btn btn-primary draggable">Popover</a>
</div>
更新 2: 我做了一个 fiddle 你可以使用它: https://jsfiddle.net/uhwrev06/
现在它完全随鼠标移动,它们之间没有 space!
更新 3: 我解决了你刚才提到的问题! https://jsfiddle.net/pe2zf1x8/