可以在单个HTML部分上为不同功能使用两个不同的JavaScript查询吗?

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

Is it possible to work two different javascript queries for different functionality on single HTML section?

问题

以下是您提供的HTML和JavaScript代码的翻译:

  1. 我已经创建了一个HTML部分。在这个部分中,我在一个div标记中使用了两个容器,并将其设置为flex,以便最终的容器会显示在左边和右边。在左边的容器中,我从上到下创建了三个卡片,而在右边的容器中,我插入了三个图片并将它们设置为绝对定位,以便它们彼此重叠。我使用了一个JavaScript查询,以便在5秒的间隔内显示其他图片,并将左边的容器设置为不可见。但我想再使用一个查询,在其中,我希望在悬停在任何卡片上时,该卡片对应的图片会变为可见。例如,如果第二/第三张图片可见,而我悬停在第一个卡片上,那么第一个图片应该出现。
  2. <!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->
  3. <!-- JavaScript 代码 -->
  4. var activeImageId = 1;
  5. var nextImageId = 1;
  6. setInterval(function() {
  7. nextImageId = nextImageId + 1;
  8. if (nextImageId < 3) {
  9. document.getElementById("img" + activeImageId).classList.replace("visible", "hidden");
  10. document.getElementById("img" + nextImageId).classList.replace("hidden", "visible");
  11. activeImageId = nextImageId;
  12. } else {
  13. document.getElementById("img" + activeImageId).classList.replace("visible", "hidden");
  14. document.getElementById("img" + nextImageId).classList.replace("hidden", "visible");
  15. activeImageId = 3;
  16. nextImageId = 0;
  17. }
  18. }, 5000);
  19. // 为第一张图片
  20. function change1() {
  21. document.getElementById("img1").src = "images/50k.png";
  22. }
  23. function change2() {
  24. document.getElementById("img1").src = "images/50k.png";
  25. }
  26. // 为第二张图片
  27. function change3() {
  28. document.getElementById("img2").src = "images/insta-pic.png";
  29. }
  30. function change4() {
  31. document.getElementById("img2").src = "images/insta-pic.png";
  32. }
  33. // 为第三张图片
  34. function change5() {
  35. document.getElementById("img3").src = "images/stats.png";
  36. }
  37. function change6() {
  38. document.getElementById("img3").src = "images/stats.png";
  39. }
  40. <!-- CSS 代码 -->
  41. .why-choose-main-container {
  42. border: 1px solid red;
  43. display: flex;
  44. align-items: center;
  45. justify-content: center;
  46. }
  47. .why-choose-main-container .why-choose-details {
  48. border: 2px solid green;
  49. }
  50. .why-choose-main-container .why-choose-details .card {
  51. border: 2px solid red;
  52. }
  53. .why-choose-main-container .why-choose-image {
  54. border: 2px solid blue;
  55. display: flex;
  56. position: relative;
  57. width: 570px;
  58. height: 670px;
  59. }
  60. .why-choose-main-container .why-choose-image img {
  61. position: absolute;
  62. width: 100%;
  63. height: 100%;
  64. }
  65. .why-choose-main-container .why-choose-image img.visible {
  66. display: block;
  67. }
  68. .why-choose-main-container .why-choose-image img.hidden {
  69. display: none;
  70. }
  71. <!-- HTML 代码 -->
  72. <div class="why-choose-main-container">
  73. <div class="why-choose-details">
  74. <div class="card" onmouseover="change1()" onmouseout="change2()">
  75. <h2>why1</h2>
  76. </div>
  77. <div class="card" onmouseover="change3()" onmouseout="change4()">
  78. <h2>why2</h2>
  79. </div>
  80. <div class="card" onmouseover="change5()" onmouseout="change6()">
  81. <h2>why3</h2>
  82. </div>
  83. </div>
  84. <div class="why-choose-image">
  85. <img src="images/50k.png" alt="img1" id="img1" class="visible">
  86. <img src="images/insta-pic.png" alt="img2" id="img2" class="hidden">
  87. <img src="images/stats.png" alt="img3" id="img3" class="hidden">
  88. </div>
  89. </div>
  90. <!-- 结束代码片段 -->

请注意,这是您提供的代码的翻译版本,其中包括HTML、JavaScript和CSS。如果您有任何其他疑问或需要进一步的帮助,请随时提出。

英文:

I have made an HTML section. In this section, I have used two containers in a div tag and made it flex so that the final containers would be visible left and right. In the left container, I made three card from top to bottom, and in the right container, I inserted the three images and made them position absolute, so that it will be on one to another. I have used one JavaScript query so that in 5 seconds intervals other images will be visible and the left should be displayed none. But I want to use one more query, and in that, I want functionality that whenever I hover on any card that card's respective image should be visible. For example, if the second/third image is visible and I hover over the first card then the first image should appear.

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

<!-- language: lang-js -->

  1. var activeImageId = 1;
  2. var nextImageId = 1;
  3. setInterval(function() {
  4. nextImageId = nextImageId + 1;
  5. if (nextImageId &lt; 3) {
  6. document.getElementById(&quot;img&quot; + activeImageId).classList.replace(&quot;visible&quot;, &quot;hidden&quot;);
  7. document.getElementById(&quot;img&quot; + nextImageId).classList.replace(&quot;hidden&quot;, &quot;visible&quot;);
  8. activeImageId = nextImageId;
  9. } else {
  10. document.getElementById(&quot;img&quot; + activeImageId).classList.replace(&quot;visible&quot;, &quot;hidden&quot;);
  11. document.getElementById(&quot;img&quot; + nextImageId).classList.replace(&quot;hidden&quot;, &quot;visible&quot;);
  12. activeImageId = 3;
  13. nextImageId = 0;
  14. }
  15. }, 5000);
  16. // for first image
  17. function change1() {
  18. document.getElementById(&quot;img1&quot;).src = &quot;images/50k.png&quot;;
  19. }
  20. function change2() {
  21. document.getElementById(&quot;img1&quot;).src = &quot;images/50k.png&quot;;
  22. }
  23. // for second image
  24. function change3() {
  25. document.getElementById(&quot;img2&quot;).src = &quot;images/insta-pic.png&quot;;
  26. }
  27. function change4() {
  28. document.getElementById(&quot;img2&quot;).src = &quot;images/insta-pic.png&quot;;
  29. }
  30. // for third image
  31. function change5() {
  32. document.getElementById(&quot;img3&quot;).src = &quot;images/stats.png&quot;;
  33. }
  34. function change6() {
  35. document.getElementById(&quot;img3&quot;).src = &quot;images/stats.png&quot;;
  36. }

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

  1. .why-choose-main-container {
  2. border: 1px solid red;
  3. display: flex;
  4. align-items: center;
  5. justify-content: center;
  6. }
  7. .why-choose-main-container .why-choose-details {
  8. border: 2px solid green;
  9. }
  10. .why-choose-main-container .why-choose-details .card {
  11. border: 2px solid red;
  12. }
  13. .why-choose-main-container .why-choose-image {
  14. border: 2px solid blue;
  15. display: flex;
  16. position: relative;
  17. width: 570px;
  18. height: 670px;
  19. }
  20. .why-choose-main-container .why-choose-image img {
  21. position: absolute;
  22. width: 100%;
  23. height: 100%;
  24. }
  25. .why-choose-main-container .why-choose-image img.visible {
  26. display: block;
  27. }
  28. .why-choose-main-container .why-choose-image img.hidden {
  29. display: none;
  30. }

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

  1. &lt;div class=&quot;why-choose-main-container&quot;&gt;
  2. &lt;div class=&quot;why-choose-details&quot;&gt;
  3. &lt;div class=&quot;card&quot; onmouseover=&quot;change1()&quot; onmouseout=&quot;change2()&quot;&gt;
  4. &lt;h2&gt;why1&lt;/h2&gt;
  5. &lt;/div&gt;
  6. &lt;div class=&quot;card&quot; onmouseover=&quot;change3()&quot; onmouseout=&quot;change4()&quot;&gt;
  7. &lt;h2&gt;why2&lt;/h2&gt;
  8. &lt;/div&gt;
  9. &lt;div class=&quot;card&quot; onmouseover=&quot;change5()&quot; onmouseout=&quot;change6()&quot;&gt;
  10. &lt;h2&gt;why3&lt;/h2&gt;
  11. &lt;/div&gt;
  12. &lt;/div&gt;
  13. &lt;div class=&quot;why-choose-image&quot;&gt;
  14. &lt;img src=&quot;images/50k.png&quot; alt=&quot;img1&quot; id=&quot;img1&quot; class=&quot;visible&quot;&gt;
  15. &lt;img src=&quot;images/insta-pic.png&quot; alt=&quot;img2&quot; id=&quot;img2&quot; class=&quot;hidden&quot;&gt;
  16. &lt;img src=&quot;images/stats.png&quot; alt=&quot;img3&quot; id=&quot;img3&quot; class=&quot;hidden&quot;&gt;
  17. &lt;/div&gt;
  18. &lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

以下是翻译好的部分:

You can my solution working here on CodePen.

HTML

  • Each image uses the onmouseover you had but I also added a onmouseout to handle leaving.
  • Each of the images calls the same JS functions but passes in its id as a parameter
  1. <div class="why-choose-main-container">
  2. <div class="why-choose-details">
  3. <div class="card" onmouseover="onHover('img1')" onmouseout="onHoverLeave('img1')">
  4. <h2>why1</h2>
  5. </div>
  6. <div class="card" onmouseover="onHover('img2')" onmouseout="onHoverLeave('img2')">
  7. <h2>why2</h2>
  8. </div>
  9. <div class="card" onmouseover="onHover('img3')" onmouseout="onHoverLeave('img3')">
  10. <h2>why3</h2>
  11. </div>
  12. </div>
  13. <div class="why-choose-image">
  14. <img src="images/50k.png" alt="img1" id="img1" class="visible">
  15. <img src="images/insta-pic.png" alt="img2" id="img2" class="hidden">
  16. <img src="images/stats.png" alt="img3" id="img3" class="hidden">
  17. </div>
  18. </div>

JS

  • The timer function keeps track of the active image but only shows it when the hovering effect is not active.
  • onHover is called whenever the mouseover event is called, which calls showHoverImage, a function that hides the active image and shows the hovered image.
  • onHoverLeave does the reverse when the onmouseout event occurs.
  1. var hoverActive = false;
  2. var activeImageId = 1;
  3. var activeImage = document.getElementById("img" + activeImageId);
  4. setInterval(function () {
  5. // Work out which image is next
  6. var nextImageId = (activeImageId % 3) + 1;
  7. var nextImage = document.getElementById("img" + nextImageId);
  8. // Change shown image
  9. if (!hoverActive) {
  10. showImage(nextImage);
  11. hideImage(activeImage);
  12. }
  13. // Next image is now active
  14. activeImage = nextImage;
  15. activeImageId = nextImageId;
  16. }, 5000);
  17. function onHover(imageName) {
  18. hoverActive = true;
  19. var hoverImage = document.getElementById(imageName);
  20. showHoverImage(hoverImage);
  21. }
  22. function showHoverImage(hoverImage) {
  23. hideImage(activeImage);
  24. showImage(hoverImage);
  25. }
  26. function onHoverLeave(imageName) {
  27. hoverActive = false;
  28. var hoverImage = document.getElementById(imageName);
  29. hideHoverImage(hoverImage);
  30. }
  31. function hideHoverImage(hoverImage) {
  32. hideImage(hoverImage);
  33. showImage(activeImage);
  34. }
  35. function showImage(docImage) {
  36. if (docImage) {
  37. docImage.classList.replace("hidden", "visible");
  38. }
  39. }
  40. function hideImage(docImage) {
  41. if (docImage) {
  42. docImage.classList.replace("visible", "hidden");
  43. }
  44. }
英文:

You can my solution working here on CodePen.

HTML

  • Each image uses the onmouseover you had but I also added a onmouseout to handle leaving.
  • Each of the images calls the same JS functions but passes in its id as a parameter
  1. &lt;div class=&quot;why-choose-main-container&quot;&gt;
  2. &lt;div class=&quot;why-choose-details&quot;&gt;
  3. &lt;div class=&quot;card&quot; onmouseover=&quot;onHover(&#39;img1&#39;)&quot; onmouseout=&quot;onHoverLeave(&#39;img1&#39;)&quot;&gt;
  4. &lt;h2&gt;why1&lt;/h2&gt;
  5. &lt;/div&gt;
  6. &lt;div class=&quot;card&quot; onmouseover=&quot;onHover(&#39;img2&#39;)&quot; onmouseout=&quot;onHoverLeave(&#39;img2&#39;)&quot;&gt;
  7. &lt;h2&gt;why2&lt;/h2&gt;
  8. &lt;/div&gt;
  9. &lt;div class=&quot;card&quot; onmouseover=&quot;onHover(&#39;img3&#39;)&quot; onmouseout=&quot;onHoverLeave(&#39;img3&#39;)&quot;&gt;
  10. &lt;h2&gt;why3&lt;/h2&gt;
  11. &lt;/div&gt;
  12. &lt;/div&gt;
  13. &lt;div class=&quot;why-choose-image&quot;&gt;
  14. &lt;img src=&quot;images/50k.png&quot; alt=&quot;img1&quot; id=&quot;img1&quot; class=&quot;visible&quot;&gt;
  15. &lt;img src=&quot;images/insta-pic.png&quot; alt=&quot;img2&quot; id=&quot;img2&quot; class=&quot;hidden&quot;&gt;
  16. &lt;img src=&quot;images/stats.png&quot; alt=&quot;img3&quot; id=&quot;img3&quot; class=&quot;hidden&quot;&gt;
  17. &lt;/div&gt;
  18. &lt;/div&gt;

JS

  • The timer function keeps track of the active image but only shows it when the hovering effect is not active
  • onHover is called whenever the mouseover event is called which calls showHoverImage, a function that hides the active image and shows the hovered image
  • onHoverLeave does the reverse when the onmouseout event occurs
  1. var hoverActive = false;
  2. var activeImageId = 1;
  3. var activeImage = document.getElementById(&quot;img&quot; + activeImageId);
  4. setInterval(function () {
  5. // Work out which image is next
  6. var nextImageId = (activeImageId % 3) + 1;
  7. var nextImage = document.getElementById(&quot;img&quot; + nextImageId);
  8. // Change shown image
  9. if (!hoverActive) {
  10. showImage(nextImage);
  11. hideImage(activeImage);
  12. }
  13. //Next image is now active
  14. activeImage = nextImage;
  15. activeImageId = nextImageId;
  16. }, 5000);
  17. function onHover(imageName) {
  18. hoverActive = true;
  19. var hoverImage = document.getElementById(imageName);
  20. showHoverImage(hoverImage);
  21. }
  22. function showHoverImage(hoverImage) {
  23. hideImage(activeImage);
  24. showImage(hoverImage);
  25. }
  26. function onHoverLeave(imageName) {
  27. hoverActive = false;
  28. var hoverImage = document.getElementById(imageName);
  29. hideHoverImage(hoverImage);
  30. }
  31. function hideHoverImage(hoverImage) {
  32. hideImage(hoverImage);
  33. showImage(activeImage);
  34. }
  35. function showImage(docImage) {
  36. if (docImage) {
  37. docImage.classList.replace(&quot;hidden&quot;, &quot;visible&quot;);
  38. }
  39. }
  40. function hideImage(docImage) {
  41. if (docImage) {
  42. docImage.classList.replace(&quot;visible&quot;, &quot;hidden&quot;);
  43. }
  44. }

答案2

得分: 0

我尝试更改了函数和JS代码。这是你的理想吗?

  1. var activeImageId = 1;
  2. var nextImageId = 1;
  3. setInterval(function() {
  4. nextImageId = nextImageId + 1;
  5. changeImgByID();
  6. }, 5000);
  7. function changeImgByID() {
  8. if (nextImageId < 3) {
  9. document.getElementById("img" + activeImageId).classList.replace("visible", "hidden");
  10. document.getElementById("img" + nextImageId).classList.replace("hidden", "visible");
  11. activeImageId = nextImageId;
  12. } else {
  13. document.getElementById("img" + activeImageId).classList.replace("visible", "hidden");
  14. document.getElementById("img" + nextImageId).classList.replace("hidden", "visible");
  15. activeImageId = 3;
  16. nextImageId = 0;
  17. }
  18. }
  19. // for first image
  20. function changeImg(val) {
  21. nextImageId=val;
  22. changeImgByID();
  23. }
  1. .why-choose-main-container {
  2. border: 1px solid red;
  3. display: flex;
  4. align-items: center;
  5. justify-content: center;
  6. }
  7. .why-choose-main-container .why-choose-details {
  8. border: 2px solid green;
  9. }
  10. .why-choose-main-container .why-choose-details .card {
  11. border: 2px solid red;
  12. }
  13. .why-choose-main-container .why-choose-image {
  14. border: 2px solid blue;
  15. display: flex;
  16. position: relative;
  17. width: 570px;
  18. height: 670px;
  19. }
  20. .why-choose-main-container .why-choose-image img {
  21. position: absolute;
  22. width: 100%;
  23. height: 100%;
  24. }
  25. .why-choose-main-container .why-choose-image img.visible {
  26. display: block;
  27. }
  28. .why-choose-main-container .why-choose-image img.hidden {
  29. display: none;
  30. }
  1. <div class="why-choose-main-container">
  2. <div class="why-choose-details">
  3. <div class="card img-1" onmouseover="changeImg(1)">
  4. <h2>why1</h2>
  5. </div>
  6. <div class="card img-2" onmouseover="changeImg(2)">
  7. <h2>why2</h2>
  8. </div>
  9. <div class="card img-3" onmouseover="changeImg(3)">
  10. <h2>why3</h2>
  11. </div>
  12. </div>
  13. <div class="why-choose-image">
  14. <img src="https://www.w3schools.com/html/pic_trulli.jpg" alt="img1" id="img1" class="visible">
  15. <img src="https://www.w3schools.com/html/img_girl.jpg" alt="img2" id="img2" class="hidden">
  16. <img src="https://www.w3schools.com/html/img_chania.jpg" alt="img3" id="img3" class="hidden">
  17. </div>
  18. </div>
英文:

I tried to change the function and JS code. Is that your ideal?

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

<!-- language: lang-js -->

  1. var activeImageId = 1;
  2. var nextImageId = 1;
  3. setInterval(function() {
  4. nextImageId = nextImageId + 1;
  5. changeImgByID();
  6. }, 5000);
  7. function changeImgByID() {
  8. if (nextImageId &lt; 3) {
  9. document.getElementById(&quot;img&quot; + activeImageId).classList.replace(&quot;visible&quot;, &quot;hidden&quot;);
  10. document.getElementById(&quot;img&quot; + nextImageId).classList.replace(&quot;hidden&quot;, &quot;visible&quot;);
  11. activeImageId = nextImageId;
  12. } else {
  13. document.getElementById(&quot;img&quot; + activeImageId).classList.replace(&quot;visible&quot;, &quot;hidden&quot;);
  14. document.getElementById(&quot;img&quot; + nextImageId).classList.replace(&quot;hidden&quot;, &quot;visible&quot;);
  15. activeImageId = 3;
  16. nextImageId = 0;
  17. }
  18. }
  19. // for first image
  20. function changeImg(val) {
  21. nextImageId=val;
  22. changeImgByID();
  23. }

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

  1. .why-choose-main-container {
  2. border: 1px solid red;
  3. display: flex;
  4. align-items: center;
  5. justify-content: center;
  6. }
  7. .why-choose-main-container .why-choose-details {
  8. border: 2px solid green;
  9. }
  10. .why-choose-main-container .why-choose-details .card {
  11. border: 2px solid red;
  12. }
  13. .why-choose-main-container .why-choose-image {
  14. border: 2px solid blue;
  15. display: flex;
  16. position: relative;
  17. width: 570px;
  18. height: 670px;
  19. }
  20. .why-choose-main-container .why-choose-image img {
  21. position: absolute;
  22. width: 100%;
  23. height: 100%;
  24. }
  25. .why-choose-main-container .why-choose-image img.visible {
  26. display: block;
  27. }
  28. .why-choose-main-container .why-choose-image img.hidden {
  29. display: none;
  30. }

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

  1. &lt;div class=&quot;why-choose-main-container&quot;&gt;
  2. &lt;div class=&quot;why-choose-details&quot;&gt;
  3. &lt;div class=&quot;card img-1&quot; onmouseover=&quot;changeImg(1)&quot;&gt;
  4. &lt;h2&gt;why1&lt;/h2&gt;
  5. &lt;/div&gt;
  6. &lt;div class=&quot;card img-2&quot; onmouseover=&quot;changeImg(2)&quot;&gt;
  7. &lt;h2&gt;why2&lt;/h2&gt;
  8. &lt;/div&gt;
  9. &lt;div class=&quot;card img-3&quot; onmouseover=&quot;changeImg(3)&quot;&gt;
  10. &lt;h2&gt;why3&lt;/h2&gt;
  11. &lt;/div&gt;
  12. &lt;/div&gt;
  13. &lt;div class=&quot;why-choose-image&quot;&gt;
  14. &lt;img src=&quot;https://www.w3schools.com/html/pic_trulli.jpg&quot; alt=&quot;img1&quot; id=&quot;img1&quot; class=&quot;visible&quot;&gt;
  15. &lt;img src=&quot;https://www.w3schools.com/html/img_girl.jpg&quot; alt=&quot;img2&quot; id=&quot;img2&quot; class=&quot;hidden&quot;&gt;
  16. &lt;img src=&quot;https://www.w3schools.com/html/img_chania.jpg&quot; alt=&quot;img3&quot; id=&quot;img3&quot; class=&quot;hidden&quot;&gt;
  17. &lt;/div&gt;
  18. &lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年8月10日 13:46:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76872919.html
匿名

发表评论

匿名网友

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

确定