我创建了一个小图像的页面,我想当它被点击,然后打开一个绝对div的大图像,这是真正的工作。但是当窗口显示在页面底部时,当我们点击小图像时,它就出现在屏幕顶部,我不喜欢它。我希望它每次都出现在屏幕中央,请在这个页面中看到它:http://www.3dcreate.ir/New3DCreate/Pages/Gallery/3DGallery/3dg_Chosen.php
我的代码示例是:
HTML代码:
<!-- This is Div of Large Image and Background Shade -->
<div class="BackgroudShade"></div>
<div class="DivOfImage"><img class="LargeImage"/></div>CSS代码:
.DivOfImage {
border: 8px solid #FFF;
color:#FFF;
background-color:#FFF;
border-radius:10px;
position:absolute;
}JQuery代码:
function LoadShowDiv () {
$('.BackgroudShade').slideDown(800); // shade of background will be visible
$(".DivOfImage").show(); // Div of Large Image will be visible
// When Image loaded successful then set width,height and top,left of Large Image Div
// but i want set top,left when screen is scroll down to bottom of page
// then show Div at middle of screen in every time
$('.LargeImage').load(function() {
$('.DivOfImage').width($('.LargeImage').width());
$('.DivOfImage').height($('.LargeImage').height());
var LeftPostion = (WidthOfScreen - $('.LargeImage').width()) / 2;
var TopPostion = (HeightOfScreen - $('.LargeImage').height()) / 2;
$(".DivOfImage").offset({ top: TopPostion, left: LeftPostion});
$('.LargeImage').show(1000);
})
}
$('#SmallImage').click(function(){
$('.LargeImage').attr('src','LargeImage.jpg');
LoadShowDiv();
})发布于 2013-12-12 17:42:06
您不需要使用所有这些JS来计算并在屏幕中间显示图像。您可以使用CSS设置样式和定位,然后使用JS隐藏/显示。您的位置:固定;然后您可以更容易地在屏幕上定义位置。
尝试如下所示:
CSS
.LargeImage {
position: fixed;
top: 50%;
left: 50%;
margin-top: -(half of image height);
margin-left: -(half of image width);
}这将把图像留在屏幕的中心
发布于 2013-12-12 17:39:22
添加left和right作为0并使用margin:0 auto
.DivOfImage {
border: 8px solid #FFF;
color:#FFF;
background-color:#FFF;
border-radius:10px;
position:absolute;
left:0;
right:0;
margin:0 auto
}这是为了使div水平居中,而不是垂直居中。
发布于 2013-12-12 17:43:40
如果你想让它在整个屏幕上水平和垂直居中,完全忽略网页上的其他元素,你可以使用
#yourElement{
position: fixed;
height: 40px;
width: 20px;
left: 50%;
top: 50%;
margin-left: -10px; //half of width
margin-top: -20px; //half of height
}这会将元素放在用户屏幕的中心。
https://stackoverflow.com/questions/20539706
复制相似问题