[英]How to make image load on mobile screens only and be responsive?
I would like to make my image load only on screens of a certain size (mobile).我想让我的图像仅在特定尺寸的屏幕(移动设备)上加载。 I would also like the image to be responsive so that it doesn't warp.我还希望图像具有响应性,以免变形。 I have written some code but it doesn't work.我写了一些代码,但它不起作用。 Right now the image takes up the entire size of my browser window and loads on all screen sizes.现在,图像占据了我的浏览器 window 的整个大小,并在所有屏幕尺寸上加载。
Does anyone have any idea on how can I fix this?有谁知道我该如何解决这个问题? Here is a fiddle of my current code.这是我当前代码的小提琴。
HTML: HTML:
<img id="yourimage" src="https://cdn.pixabay.com/photo/2016/05/02/22/16/apple-blossoms-1368187_960_720.jpg">
<p>
Why does the image warp when resizing why doesn't it only open on 500px screens and lower?
</p>
CSS: CSS:
#yourimage {
display: none;
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
width: 50%;
height: 50%;
}
@media (max-width: 500px) {
#yourimage {
display: block;
}
}
JS: JS:
function showPopup() {
document.getElementById('yourimage').style.display = 'block';
}
showPopup(); // show modal image.
function closePopUp() {
document.getElementById('yourimage').style.display = 'none';
}
document.getElementById('yourimage').addEventListener('click', closePopUp); // hide modal image
Just remove the call of the showPopup()
javascript function.只需删除showPopup()
javascript function 的调用。 I also recommend you to use @media only screen and (max-width: 500px)
instead of just @media (max-width: 500px)
:我还建议您使用@media only screen and (max-width: 500px)
而不是@media (max-width: 500px)
:
function showPopup() { document.getElementById('yourimage').style.display = 'block'; } function closePopUp() { document.getElementById('yourimage').style.display = 'none'; } document.getElementById('yourimage').addEventListener('click', closePopUp); // hide modal image
#yourimage { display: none; position: absolute; top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); -moz-transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); -o-transform: translate(-50%, -50%); transform: translate(-50%, -50%); width: 50%; height: 50%; } @media only screen and (max-width: 500px) { #yourimage { display: block; } }
<img id="yourimage" src="https://cdn.pixabay.com/photo/2016/05/02/22/16/apple-blossoms-1368187_960_720.jpg"> <p> Why does the image warp when resizing why doesn't it only open on 500px screens and lower? </p>
Remove showPopup()
from js, then it will work correctly从js中去掉showPopup()
,就可以正常工作了
Your problem is that javascript overwrites the display: block
inside the css with the display = 'block'
which inserts with showPopup ();
您的问题是 javascript 覆盖了display: block
在 css 内, display = 'block'
插入showPopup ();
function showPopup() { document.getElementById('yourimage').style.display = 'block'; } // showPopup(); function closePopUp() { document.getElementById('yourimage').style.display = 'none'; } document.getElementById('yourimage').addEventListener('click', closePopUp); // hide modal image
#yourimage { display: none; position: absolute; top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); -moz-transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); -o-transform: translate(-50%, -50%); transform: translate(-50%, -50%); width: 50%; height: 50%; } @media (max-width: 500px) { #yourimage { display: block; } }
HTML: <img id="yourimage" src="https://cdn.pixabay.com/photo/2016/05/02/22/16/apple-blossoms-1368187_960_720.jpg"> <p> Why does the image warp when resizing why doesn't it only open on 500px screens and lower? </p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.