简体   繁体   English

如何仅在移动屏幕上加载图像并做出响应?

[英]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.

相关问题 如何使 Vuetify 轮播图像响应,尤其是在移动屏幕上? - how do I make Vuetify carousel images responsive especially on mobile screens? 模态对移动设备没有响应,如何使其在移动设备上响应? - Modal is not responsive for mobile, How to make it responsive on mobile? 如何使我的 png 图像和文本容器响应不同尺寸的屏幕? - How to make my png image and text container responsive to different size screens? 如何制作响应式图像? - How to make responsive image? 如何使 header 仅在大屏幕上具有粘性,但在用户向下滚动时在移动设备上消失? - How can I make the header be sticky only on large screens but disappears on mobile as the user scrolls down? 如何在小屏幕上触发此jQuery移动导航脚本? - How do I make this jQuery mobile navigation script trigger only on small screens? 使标签在较小的屏幕上响应 - Make tabs responsive on smaller screens 怎么做 <iframe> 在wordpress中响应。 我只能在移动设备或桌面设备上做出响应。 需要它在两者中都运作良好 - how to make <iframe> responsive in wordpress. I can only make it responsive in either mobile or in desktop. need it to work well in both 如何使图片轮播响应在移动尺寸上显示1张图片? - How can I make my image carousel responsive to display 1 image on mobile size? CSS / Javascript-适用于较小屏幕的移动响应菜单 - CSS / Javascript - Mobile responsive menu for smaller screens
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM