html 如何使一个无限的股票代码线只有一个图像?

gr8qqesn  于 2023-11-15  发布在  其他
关注(0)|答案(3)|浏览(125)

我几乎按预期工作。然而,由于只有一个图像,当动画到左侧时,右侧会打开一个间隙。我需要的是,股票代码行总是用相同的图像填充,没有任何间隙。如果图像比容器窄,它应该向左和向右重复。
任何帮助或指导都非常感谢。

  1. const tickerImage = document.getElementById('ticker-image');
  2. const tickerLink = document.getElementById('ticker-link');
  3. let isRotating = true;
  4. tickerLink.addEventListener('click', function(e) {
  5. if (isRotating) {
  6. e.preventDefault(); // Prevent the link from navigating if it's rotating
  7. }
  8. });
  9. tickerLink.addEventListener('mouseover', function() {
  10. isRotating = false;
  11. tickerImage.style.animationPlayState = 'paused';
  12. });
  13. tickerLink.addEventListener('mouseout', function() {
  14. isRotating = true;
  15. tickerImage.style.animationPlayState = 'running';
  16. });
  1. body {
  2. margin: 0;
  3. padding: 0;
  4. overflow: hidden;
  5. }
  6. .ticker {
  7. position: fixed;
  8. bottom: 0;
  9. left: 0;
  10. width: 100%;
  11. background-color: #333;
  12. color: #fff;
  13. padding: 10px 0;
  14. text-align: center;
  15. cursor: pointer;
  16. user-select: none;
  17. overflow: hidden;
  18. }
  19. .ticker-content {
  20. text-decoration: none;
  21. display: block;
  22. width: 100%;
  23. /* Set the width to 100% */
  24. height: 100%;
  25. /* Set the height to 100% */
  26. position: relative;
  27. overflow: hidden;
  28. }
  29. .image-container {
  30. display: flex;
  31. width: 100%;
  32. white-space: nowrap;
  33. animation: slideImage 10s linear infinite;
  34. }
  35. .image-container img {
  36. display: inline-block;
  37. width: 100%
  38. }
  39. @keyframes slideImage {
  40. 0% {
  41. transform: translateX(100%);
  42. }
  43. 100% {
  44. transform: translateX(-100%);
  45. }
  46. }
  1. <div class="ticker">
  2. <a href="https://example.com" target="_blank" class="ticker-content" id="ticker-link">
  3. <div class="image-container">
  4. <img src="https://images.all-free-download.com/images/graphiclarge/beach_cloud_dawn_horizon_horizontal_landscape_ocean_601821.jpg" alt="Rotating Image" id="ticker-image">
  5. </div>
  6. </a>
  7. </div>
2jcobegt

2jcobegt1#

background-size:cover;您可以在.tickercontent中尝试此属性

k75qkfdt

k75qkfdt2#

  1. * {
  2. overflow: hidden;
  3. }
  4. .test {
  5. display: flex;
  6. background-image: url("https://images.all-free-download.com/images/graphiclarge/beach_cloud_dawn_horizon_horizontal_landscape_ocean_601821.jpg");
  7. background-repeat: no-repeat;
  8. background-size: contain;
  9. }
  10. #ticker-image {
  11. width: 100%;
  12. animation: slideImage 10s linear infinite;
  13. }
  14. @keyframes slideImage {
  15. 0% {
  16. transform: translateX(100%);
  17. }
  18. 100% {
  19. transform: translateX(-100%);
  20. }
  21. }

个字符

展开查看全部
hgc7kmma

hgc7kmma3#

JavaScript和CSS来创建一个从右向左动画的图像的滚动条效果。我注意到你只使用了一个图像元素,这会导致当它到达容器的左侧时出现一个间隙。
要解决此问题,您可以尝试使用两个图像元素而不是一个,并将它们放置在彼此旁边。然后,您可以使用CSS动画将它们都向左移动一个图像的宽度,然后将它们的位置重置为右侧。这样,您将始终在屏幕上看到一个图像,而另一个可以随时替换它。
下面是一个示例,说明如何执行此操作:
第一个月

  1. <div id="ticker-container">
  2. <a id="ticker-link" href="https://www.dummyLink.com">
  3. <img id="ticker-image-1" src="https://www.dummy.com" alt="logo">
  4. <img id="ticker-image-2" src="https://www.dummy.com" alt=" logo">
  5. </a>
  6. </div>

字符串
CSS file :

  1. #ticker-container {
  2. width: 300px;
  3. height: 100px;
  4. overflow: hidden;
  5. }
  6. #ticker-link {
  7. display: flex;
  8. align-items: center;
  9. }
  10. #ticker-image-1,
  11. #ticker-image-2 {
  12. width: 300px;
  13. height: auto;
  14. animation: ticker 5s linear infinite;
  15. }
  16. #ticker-image-2 {
  17. margin-left: -300px; /* Position the second image to the left of the first one */
  18. }
  19. @keyframes ticker {
  20. 0% {
  21. transform: translateX(0); /* Start from the right */
  22. }
  23. 100% {
  24. transform: translateX(-300px); /* Move to the left by the width of one image */
  25. }
  26. }


Js file :

  1. const tickerLink = document.getElementById('ticker-link');
  2. const tickerImage1 = document.getElementById('ticker-image-1');
  3. const tickerImage2 = document.getElementById('ticker-image-2');
  4. let isRotating = true;
  5. tickerLink.addEventListener('click', function(e) {
  6. if (isRotating) {
  7. e.preventDefault(); // Prevent the link from navigating if it's rotating
  8. }
  9. });
  10. tickerLink.addEventListener('mouseover', function() {
  11. isRotating = false;
  12. tickerImage1.style.animationPlayState = 'paused';
  13. tickerImage2.style.animationPlayState = 'paused';
  14. });
  15. tickerLink.addEventListener('mouseout', function() {
  16. isRotating = true;
  17. tickerImage1.style.animationPlayState = 'running';
  18. tickerImage2.style.animationPlayState = 'running';
  19. })


;

展开查看全部

相关问题