将一张图片从左到右移动,然后再从左到右移动。

huangapple go评论114阅读模式
英文:

Move an image from left to right, then again from left to right

问题

I am working on a css for some images, basically what i want to achieve is that there is a image it should come out of the left side move towards the right side, when it reaches the right end it should again come out of the left side and so on, i was able to achieve this using the following code.

@keyframes slideAnimation {
  0% {
    transform: translateX(0%);
  }
  100% {
    transform: translateX(100%);
  }
}

.row-1 {
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  width: 100%;
  height: 100%;
}

.image-container {
  position: absolute;
  top: 4%;
  width: 100vw !important;
  overflow: hidden;
  animation: slideAnimation 18s linear infinite;
}
<div class="row-1">
  <div class="image-container">
    <img class="img" src="/src/assets/image.svg" alt="img">
  </div>
</div>

This works for desktop and not for mobile css it adds scroll to my page even though the image comes out of left again when it reaches the right side but the image container's width is 100vw, and image is in the start of the container and when image reaches the extreme right side there is a scroll that because of the container width, which is effecting my other absolute elements on the page, can i somehow disable that scroll, or achieve the same thing using some different way because its effecting my other absolute elements as well.

英文:

I am working on a css for some images, basically what i want to achieve is that there is a image it should come out of the left side move towards the right side, when it reaches the right end it should again come out of the left side and so on, i was able to achieve this using the following code.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

@keyframes slideAnimation {
  0% {
    transform: translateX(0%);
  }
  100% {
    transform: translateX(100%);
  }
}

.row-1 {
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  width: 100%;
  height: 100%;
}

.image-container {
  position: absolute;
  top: 4%;
  width: 100vw !important;
  overflow: hidden;
  animation: slideAnimation 18s linear infinite;
}

<!-- language: lang-html -->

&lt;div class=&quot;row-1&quot;&gt;
  &lt;div class=&quot;image-container&quot;&gt;
    &lt;img class=&quot;img&quot; src=&quot;/src/assets/image.svg&quot; alt=&quot;img&quot;&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

This works for desktop and not for mobile css it adds scroll to my page even though the image comes out of left again when it reaches the right side but the image container's width is 100vw, and image is in the start of the container and when image reaches the extreme right side there is a scroll that because of the container width, which is effecting my other absolute elements on the page, can i somehow disable that scroll, or achieve the same thing using some different way because its effecting my other absolute elements as well.

答案1

得分: 1

你面临的问题是由于动画元素移出了视口,导致页面滚动。防止这种情况的一种方法是隐藏父容器上的溢出内容。但是,由于你提到这会影响页面上其他绝对定位的元素,另一种方法可能是使用CSS transform属性而不是改变left或right属性。

@keyframes slideAnimation {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(100%);
  }
}

.row-1 {
  overflow: hidden;
  position: relative;
  width: 100%;
  height: 100%;
}

.image-container {
  position: absolute;
  top: 4%;
  width: 100%;
  animation: slideAnimation 18s linear infinite;
}

slideAnimation关键帧使用translateX将图像从左到右移动。图像从屏幕外部(-100%)开始,移动到屏幕右侧(100%)。在.row-1类上的overflow: hidden将防止动画导致的任何水平滚动。

这应该能够实现你所期望的效果,而不会引起任何不必要的滚动。

英文:

The issue you're facing is due to the fact that the animated element is moving outside the viewport, causing the page to scroll. One way to prevent this is to hide the overflow on the parent container. However, since you mentioned that this is affecting other absolute elements on your page, another approach might be to use CSS transform property instead of changing the left or right properties.

@keyframes slideAnimation {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(100%);
  }
}

.row-1 {
  overflow: hidden;
  position: relative;
  width: 100%;
  height: 100%;
}

.image-container {
  position: absolute;
  top: 4%;
  width: 100%;
  animation: slideAnimation 18s linear infinite;
}

The slideAnimation keyframes are using translateX to move the image from left to right. The image starts off the screen (-100%) and moves to the right side of the screen (100%). The overflow: hidden on the .row-1 class will prevent any horizontal scrolling caused by the animation.

This should give you the desired effect without causing any unwanted scrolling

答案2

得分: 0

已经在这里回答了如何使元素移动:https://stackoverflow.com/questions/15464055/css-transition-effect-makes-image-blurry-moves-image-1px-in-chrome

至于重新设置回左侧,请考虑编写一个脚本,检查图像位置是否超出窗口的像素宽度,如果是,则将其重置回左侧。

英文:

Getting elements to move at all has been answered here: https://stackoverflow.com/questions/15464055/css-transition-effect-makes-image-blurry-moves-image-1px-in-chrome

As for resetting back to the left hand side, have you considered a script that checks if the image's location exceeds the pixel width of the window, and if so, resets it back to the left hand side?

huangapple
  • 本文由 发表于 2023年6月12日 20:48:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76456832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定