有一个名为“dvUsers”的div。有一个锚标签“lnkUsers”。
当点击anchortag时,div必须像它下方的弹出式div一样打开。
此外,div的相对位置应保持在窗口调整大小和所有。 如何使用javascript / jquery?
答案 0 :(得分:2)
也许您应该寻找像 overLIB 这样的预制脚本:http://www.bosrup.com/web/overlib/! - )
答案 1 :(得分:2)
我的偏好是将这两个放在父div中,如下所示:
<div id="container">
<a id="lnkUsers" href="#">Users</a>
<div id="dvUsers" style="display: none;">
<!-- user content... -->
</div>
</div>
这些元素的CSS将是:
#container{
/* ensure that #dvUsers is positioned relatively to this element */
position: relative;
}
#dvUsers{
position: absolute;
/* this value should be based on the font-size of #lnkUsers */
top: 30px;
left: -10px;
}
这可确保div相对于链接正确定位。 (为了这个问题,我假设父div是“text-align:left”或者浮动)
javascript看起来像这样:
$(function(){
$('#lnkUsers').click(function(){
$('#dvUsers').slideToggle();
});
});
答案 2 :(得分:0)
$(document).ready(function(){ $("#lnkUsers").click(function(){ $("#dvUser").show("slow"); });
首先应将style =“display:none”应用于dvUser,使其不可见。
答案 3 :(得分:0)
您可以使用jQuery插件PositionCalculator。
HTML:
<p><a class="lnkUsers" href="javascript:void(0)">Users Link1</a></p>
<p><a class="lnkUsers" href="javascript:void(0)">Users Link 2</a></p>
<p><a class="lnkUsers" href="javascript:void(0)">Users Link 3</a></p>
<p><a class="lnkUsers" href="javascript:void(0)">Users Link 4</a></p>
<p><a class="lnkUsers" href="javascript:void(0)">Users Link 5</a></p>
<p><a class="lnkUsers" href="javascript:void(0)">Users Link 6</a></p>
<p><a class="lnkUsers" href="javascript:void(0)">Users Link 7</a></p>
<p><a class="lnkUsers" href="javascript:void(0)">Users Link 8</a></p>
<p><a class="lnkUsers" href="javascript:void(0)">Users Link 9</a></p>
<div id="dvUsers" style="display:none; position:absolute; padding:10px; background:rgba(0,0,0,0.8); color:#ffffff">
I am the popup. <b>Click me to close</b>
<div class="dynamicInfo"></div>
</div>
JavaScript的:
jQuery(function($) {
var $popup = $('#dvUsers');
var $infoField = $popup.find('.dynamicInfo');
function showPopup(event) {
// set content
$infoField.text('clicked link: ' + $(this).text());
// reset position
$popup.show().css({top: 0, left: 0});
// calculate new position
var calculator = new $.PositionCalculator({
item: $popup,
itemAt: "top left",
target: this,
targetAt: "bottom left",
flip: "both"
});
var posResult = calculator.calculate();
// set new position
$popup.css({
top: posResult.moveBy.y + "px",
left: posResult.moveBy.x + "px"
});
// window resize handler
$(window).off('resize.dvUsers');
$(window).on('resize.dvUsers', function(event) {
$popup.css({top: 0, left: 0});
var newResult = calculator.resize().calculate();
$popup.css({
top: newResult.moveBy.y + "px",
left: newResult.moveBy.x + "px"
});
});
}
// add click handler for show and hide
$('.lnkUsers').on('click', showPopup);
$popup.on('click', function() {
$popup.hide();
$(window).off('resize.dvUsers');
});
});