英文:
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 -->
<div class="row-1">
<div class="image-container">
<img class="img" src="/src/assets/image.svg" alt="img">
</div>
</div>
<!-- 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?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论