我正在为一个类项目的网页工作,我已经设置了框架,但是我遇到了一个问题。我不能让这个图像居中对齐我的生活。它当前位于屏幕的右侧,我无法让它移动。您很可能需要在浏览器中粘贴此内容以准确表示正在发生的事情。
initComparisons();
function initComparisons() {
var x, i;
/*find all elements with an "overlay" class:*/
x = document.getElementsByClassName("img-comp-overlay center");
for (i = 0; i < x.length; i++) {
/*once for each "overlay" element:
pass the "overlay" element as a parameter when executing the compareImages function:*/
compareImages(x[i]);
}
function compareImages(img) {
var slider, img, clicked = 0, w, h;
/*get the width and height of the img element*/
w = img.offsetWidth;
h = img.offsetHeight;
/*set the width of the img element to 50%:*/
img.style.width = (w / 2) + "px";
/*create slider:*/
slider = document.createElement("DIV");
slider.setAttribute("class", "img-comp-slider");
/*insert slider*/
img.parentElement.insertBefore(slider, img);
/*position the slider in the middle:*/
slider.style.top = (h / 2) - (slider.offsetHeight / 2) + "px";
slider.style.left = (w / 2) - (slider.offsetWidth / 2) + "px";
/*execute a function when the mouse button is pressed:*/
slider.addEventListener("mousedown", slideReady);
/*and another function when the mouse button is released:*/
window.addEventListener("mouseup", slideFinish);
/*or touched (for touch screens:*/
slider.addEventListener("touchstart", slideReady);
/*and released (for touch screens:*/
window.addEventListener("touchstop", slideFinish);
function slideReady(e) {
/*prevent any other actions that may occur when moving over the image:*/
e.preventDefault();
/*the slider is now clicked and ready to move:*/
clicked = 1;
/*execute a function when the slider is moved:*/
window.addEventListener("mousemove", slideMove);
window.addEventListener("touchmove", slideMove);
}
function slideFinish() {
/*the slider is no longer clicked:*/
clicked = 0;
}
function slideMove(e) {
var pos;
/*if the slider is no longer clicked, exit this function:*/
if (clicked == 0) return false;
/*get the cursor's x position:*/
pos = getCursorPos(e)
/*prevent the slider from being positioned outside the image:*/
if (pos < 0) pos = 0;
if (pos > w) pos = w;
/*execute a function that will resize the overlay image according to the cursor:*/
slide(pos);
}
function getCursorPos(e) {
var a, x = 0;
e = e || window.event;
/*get the x positions of the image:*/
a = img.getBoundingClientRect();
/*calculate the cursor's x coordinate, relative to the image:*/
x = e.pageX - a.left;
/*consider any page scrolling:*/
x = x - window.pageXOffset;
return x;
}
function slide(x) {
/*resize the image:*/
img.style.width = x + "px";
/*position the slider:*/
slider.style.left = img.offsetWidth - (slider.offsetWidth / 2) + "px";
}
}
}
body {
margin: 0;}
#bg {
position: fixed;
top: 0;
left: 0;
/* Preserve aspet ratio */
min-width: 100%;
min-height: 100%;
}
ul.topnav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
background-color: rgba(0,255,255, 0.3);
backdrop-filter: blur(5px);
-webkit-backdrop-filter: blur(5px);
max-width: 100%;
max-height: 100%;
overflow: auto;
}
.center {
display: flex;
justify-content: center;
flex-direction: column;
}
ul.topnav li {float: left;}
ul.topnav li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
ul.topnav li a:hover:not(.active) { background-color: rgba(47,79,79, 0.6);
backdrop-filter: blur(5px);
-webkit-backdrop-filter: blur(5px);}
ul.topnav li a.active {background-color: rgba(112,128,144, 0.6);
backdrop-filter: blur(5px);
-webkit-backdrop-filter: blur(5px);}
ul.topnav li.right {float: right;}
@media screen and (max-width: 600px){
ul.topnav li.right,
ul.topnav li {float: none;}
}
.tanddblur {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
align-content: center;
align-self:center;
background-color: #333;
background-color: rgba(47,79,79, 0.5);
backdrop-filter: blur(5px);
-webkit-backdrop-filter: blur(5px);
overflow: auto;
padding-left:10px;
padding-right:10px;
border-style: solid;
border-color: darkslategray;
border-radius:15px;
padding-top:10px;
padding-bottom:10px;
}
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
* {box-sizing: border-box;}
.img-comp-container {
position: absolute;
height: 200px; /*should be the same height as the images*/
top: 50%;
left: 50%;
right:50%;
transform: translate(-50%, -50%);
}
.img-comp-img {
position: absolute;
width: auto;
height: auto;
overflow: hidden;
}
.img-comp-img img {
display: block;
vertical-align: middle;
}
.img-comp-slider {
position:absolute;
https://www.w3schools.com/howto/img_fjords.jpg z-index: 9;
cursor: ew-resize;
/*set the appearance of the slider:*/
width: 40px;
height: 40px;
background-color: #2196F3;
opacity: 0.7;
border-radius: 50%;
}
#display_image
{
align-self:center;
}
<img src="webpagepic.jpg" id="bg" alt="">
<ul class="topnav">
<li><a class= "active" href="#news">Reenacted Photos</a></li>
<li><a href="navpage.html">Terms & Definitions</a></li>
<li><a href="#contact">Video</a></li>
<li><a href="#about">Review Links</a></li>
<li class="right"><a href="index.html">Return Home</a></li>
</ul>
<div align= 'center' class="img-comp-container center">
<div align= 'center' class="img-comp-img">
<img src="https://www.w3schools.com/howto/img_forest.jpg" width="600" height="300">
</div>
<div align= 'center' class="img-comp-img img-comp-overlay center">
<img src="https://www.w3schools.com/howto/img_fjords.jpg" width="600" height="300">
</div>
</div>
<div>
答案 0 :(得分:0)
对您的CSS进行以下更改。基本上,分配left
百分比会相对于容器的宽度移动图像,top
百分比会相对于父容器的高度移动图像。我使用calc来完美地居中图像。例如,容器宽度为600px,因此水平中心为50% - 300px(图像宽度的一半)。
最后,我为html和body指定了一个宽度和高度,以便正文显示浏览器窗口的长度和宽度。
.img-comp-container {
position: absolute;
left: calc(50% - 300px);
top: calc(50% - 23px);
height: 200px; /*should be the same height as the images*/
width: 600px;
}
html,body{
width:100%;
height:100%;
margin:0;
}