简体   繁体   English

即使屏幕分辨率发生变化,如何使图像在页面上移动

[英]How do i make my image move across the page even when the resolution of screen changes

I have an image which goes from one side off the screen to other. 我有一幅图像从屏幕的一侧移到另一侧。 However, when I open the HTML on a different sized computer/laptop, it does not fit and looks out of place. 但是,当我在其他尺寸的计算机/笔记本电脑上打开HTML时,它不合适并且看起来不合适。 How do I fix this? 我该如何解决?

CODE: 码:

 body { text-align: center; } div.container { text-align: left; width: 710px; margin: 0 auto; border: 12px solid black; border-radius: 10px; } div.content { width: 700px; min-height: 400px; background-color: white; padding: 5px; } @-webkit-keyframes mini { from { left: 410px; } } .mini { position: absolute; top: 280px; left: 950px; width: 166px; height: 70px; z-index: 10000; -webkit-animation: mini 3s; animation: mini 8s; } 
 <div class="container"> <div class="content"> <img src="Media/buscartoon.jpg" class="mini" /> </div> </div> 

maybe set initial left and top values 也许设置初始的左和上限值

 .imganim {
        width:100px;
        height:60px;
        position:absolute;
        -webkit-animation:myfirst 5s;
        animation:myfirst 5s;
        left: 0;
        top: 0;
    }

Your .content and .container have no position set, so I guess it's defaulting to the next parent element that does have these set. 您的.content和.container没有设置位置,所以我想它默认是下一个具有这些设置的父元素。

Pop this on your .content div: 在您的.content div上弹出:

position: relative;

the image is still going to go over the limits because of left: 100% but adding a relative position to the container may well help you get to the next problem. 由于左图,图片仍然会超出限制:100%,但是在容器中添加相对位置可能会帮助您解决下一个问题。

If you want the image to sit flush with the edge of the container rather than running over, you can also change your left: 100% to: 如果要使图像与容器的边缘齐平而不是溢出,也可以将左:100%更改为:

left: calc(100% - 100px)

...where 100px is the width of the element. ...其中100px是元素的宽度。

edit: jsfiddle example https://jsfiddle.net/w56r2xnr/ 编辑:jsfiddle示例https://jsfiddle.net/w56r2xnr/

Try the following css classes that i have ammended. 尝试以下我已修订的CSS类。 I have kept the top at 5px which makes room for the 5px padding within the content div. 我将顶部保持在5px,这为content div中的5px填充留出了空间。 Also the 50% transformation formal includes the left 100% - (width of the image + right-padding). 同样,50%转换形式包括左100%-(图像宽度+右填充)。

You can now adjust the top to make it as you see fit. 现在,您可以调整顶部以使其适合您的需要。

CSS changes: CSS更改:

 div.content {
    width: 700px; min-height: 400px;
    background-color: white; padding: 5px; 
    position: relative;
 }

    /* Chrome, Safari, Opera */
    @-webkit-keyframes myfirst
    {
       0%   {left:0%; top:5px;}
        50%  {left: calc(100% - 105px);}
        100% {left:0%; top:5px;}
    }

    /* Standard syntax */
    @keyframes myfirst
    {
         0%   { left:0%; top:5px;}
        50%  {left: calc(100% - 105px);}
        100% {left:0%; top:5px;}
    }

Sample: http://codepen.io/Nasir_T/pen/ZBpjpw Hope this helps. 示例: http : //codepen.io/Nasir_T/pen/ZBpjpw希望这会有所帮助。

[Edit - Code changed in question] [编辑-代码已更改]

I think in both scenarios you will need to set the content div with position:relative to keep the image contained within it as the image itself is position:absolute . 我认为在这两种情况下,您都需要将content div设置为position:relative以使图像包含在其中,因为图像本身就是position:absolute Along with that you need to use percentage values for the left and top in order for the animation and the position to be in the right place regardless of the size of the screen. 除此之外,您还需要在左侧和顶部使用百分比值,以使动画和位置位于正确的位置,而不管屏幕的大小如何。

For the updated code in question please check the following code sample: 对于有问题的更新代码,请检查以下代码示例:

http://codepen.io/Nasir_T/pen/ObRwmO http://codepen.io/Nasir_T/pen/ObRwmO

Just adjust the key frame left percentage according to your need. 只需根据需要调整关键帧左百分比。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM