简体   繁体   English

我可以将 onmouseover 用于 div 吗?

[英]Can I use onmouseover for a div?

I wanted to create a slideshow with some images inside a div.我想在 div 中创建一个包含一些图像的幻灯片。 The slides will change continuously on hover over the slide.幻灯片将在幻灯片上方的 hover 上不断变化。 The div will appear in a position at the top-left corner of the webpage.该 div 将出现在网页左上角的 position 中。 There are other divs and functionalities in the web page as well. web 页面中还有其他 div 和功能。

I used this section of code inside the div for slideshow.我在 div 中使用这部分代码进行幻灯片放映。 It works when hover over the dots.它在 hover 在点上时起作用。 I want it to happen when I hover over slide as well.我希望它发生在我 hover 在幻灯片上时。 I have used onmouseover inside div, but it isn't working.我在 div 中使用了 onmouseover,但它不起作用。 How can I do that?我怎样才能做到这一点?

The rest classes are just for positioning and size of the div. rest 类仅用于 div 的定位和大小。

html html

<div role="list" class="banner__list w-dyn-items">
  <div style="background-image:url(/frontEnd/5a10aaa4d85f4b0001a5419a_2017-lamborghini-huracan-    spyder-orange-exterior-front-angle-royalty-exotic-cars.jpg)" role="listitem" class="banner__item w-dyn-   item mySlides" onmouseover="currentSlide(1)">
  </div>
  <div style="background-image:url(/frontEnd/5a10aaa4d85f4b0001a541d8_2015-bentley-continental-gtc-  red-car-hero-2-image-royalty-exotic-cars.jpg)" role="listitem" class="banner__item w-dyn-item mySlides" onmouseover="currentSlide(2)">
  </div>
  <div style="background-image:url(/frontEnd/5abda7fc38a916291e1647d3_2018-jeep-wrangler-white-isolated-front-angle-royalty-exotic-cars.jpg)" role="listitem" class="banner__item w-dyn-item mySlides" onmouseover="currentSlide(3)">
  </div>
  <span class="dot" onmouseover="currentSlide(1)"></span>
  <span class="dot" onmouseover="currentSlide(2)"></span>
  <span class="dot" onmouseover="currentSlide(3)"></span>
</div>

javascript javascript

var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
  showSlides(slideIndex += n);
}

function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("dot");
  if (n > slides.length) {
    slideIndex = 1
  }
  if (n < 1) {
    slideIndex = slides.length
  }
  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";
  }
  for (i = 0; i < dots.length; i++) {
    dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[slideIndex - 1].style.display = "block";
  dots[slideIndex - 1].className += " active";
 }

Currently slide changes when I hover over the dots.当前,当我在点上 hover 时,幻灯片会发生变化。 I want the slide to continuously go to the next slide as long as that hover is maintained over the slide as well.我希望幻灯片连续 go 到下一张幻灯片,只要 hover 也保持在幻灯片上。 How can I do that?我怎样才能做到这一点?

Use a container element - onmouseenter and onmouseleave events使用容器元素 - onmouseenter 和 onmouseleave 事件

You can use the mouseover event, although when you enter the next slide it triggers the event once more.您可以使用 mouseover 事件,但当您进入下一张幻灯片时,它会再次触发该事件。 I suggest the use of container element with onmouseenter and onmouseleave events.我建议使用带有 onmouseenter 和 onmouseleave 事件的容器元素。 When the mouse enters the container you start the slide show and stop when it leaves.当鼠标进入容器时,您开始幻灯片放映并在离开时停止。

See Demo: https://jsfiddle.net/hexzero/03jvw52u/见演示: https://jsfiddle.net/hexzero/03jvw52u/

var slideShowInterval;                 // Interval value is stored here

function slideShow(start) {
  if (start) {
    currentSlide((slideIndex += 1));   // First call before the Interval starts
    slideShowInterval = setInterval(function () {
      currentSlide((slideIndex += 1)); // This call currentSlide function every 700ms
    }, 700);                           //  700 is the delay before the next slide
  } else {
    clearInterval(slideShowInterval);  // Stop interval 
  }
}
<body>
  <section>
    <div role="list" class="banner__list w-dyn-items">
      <div                              <!-- Add Container element -->
        class="image-container"          
        onmouseenter="slideShow(true)"  <!-- And event triggers with a function call -->
        onmouseleave="slideShow(false)" 
      >
        <div
          style="background: blue;"
          role="listitem"
          class="banner__item w-dyn- item mySlides" <!-- Remove onmouseover -->
        ></div>
        <div
          style="background: black;"
          role="listitem"
          class="banner__item w-dyn-item mySlides" <!-- Remove onmouseover -->
        ></div>
        <div
          style="background: red;"
          role="listitem"
          class="banner__item w-dyn-item mySlides" <!-- Remove onmouseover -->
        ></div>
      </div>
      <span class="dot" onmouseover="currentSlide(1)"></span>
      <span class="dot" onmouseover="currentSlide(2)"></span>
      <span class="dot" onmouseover="currentSlide(3)"></span>
    </div>
  </section>
</body>

If you have the JQuery library, simple use this:如果你有 JQuery 库,简单地使用这个:

$(".mySlides:nth-child(1)").mouseover(function() {
    currentSlide(1);
});
$(".mySlides:nth-child(2)").mouseover(function() {
    currentSlide(2);
});
$(".mySlides:nth-child(3)").mouseover(function() {
    currentSlide(3);
});

or或者

var mySlides = $(".mySlides");
for (var i=0; i<mySlides.length; i++) {
    $(mySlides[i]).mouseover(function() {
        currentSlide(i+1);
    });
}

This adds a mouseover event to the div - instead of using the onmouseover attribute.这将mouseover事件添加到 div - 而不是使用onmouseover属性。

//from w3school where you originally got your code I guess

var slideIndex = 0;
showSlides();

function showSlides() {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("dot");
  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";  
  }
  slideIndex++;
  if (slideIndex > slides.length) {slideIndex = 1}    
  for (i = 0; i < dots.length; i++) {
    dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[slideIndex-1].style.display = "block";  
  dots[slideIndex-1].className += " active";
  setTimeout(showSlides, 2000); // Change image every 2 seconds
}

做了一个<div> display:block with onmouseover() 但我无法点击它,因为一旦我将鼠标移开,它就会因为 onmouseout() 而消失</div><div id="text_translate"><p>所以我做了一个 display: block 当鼠标悬停在某个时,并 display: none 当 cursor 移开时。 '</p><p> <a href="https://i.stack.imgur.com/hu3Zz.png" rel="nofollow noreferrer">我制作的 div 仅在鼠标悬停在某个链接上时显示</a></p><p> <a href="https://i.stack.imgur.com/afbPC.png" rel="nofollow noreferrer">div 有一个 display: none 当鼠标离开链接时</a></p><p>这是我用过的代码</p><p> HTML:</p><pre> &lt;a href="#" onmouseover="LoginShow()" onmouseout="LoginHide()"&gt;Login/Sign Up&lt;/a&gt;</pre><p> JavaScript:</p><pre> function LoginShow (){ document.getElementById("log").style.display="block";} function LoginHide(){ document.getElementById("log").style.display="none";}</pre><p> 但是我无法单击 div,因为一旦我尝试将 cursor 移动到 div 中的按钮,则 div 将不显示任何内容,因为我必须将 cursor 远离链接。</p><p> 我是 JS 新手,但我见过其他 web 页面这样做,div 在鼠标悬停时显示的方式是什么,可以单击并显示:只有当我离开 div 时才显示。</p><p> 我也试过</p><pre> &lt;a href="#" onmouseover="LoginShow()"&gt;Login/Sign Up&lt;/a&gt; &lt;div class="login" id="log" onmouseover="LoginShow()" onmouseout="LoginHide()"&gt;</pre><p> It kind of solves the problem, but for the div to go to display none I have to move the cursor away from the div, if the move the cursor away from the anchor tag, it doesn't go away.</p></div> - made a <div> display:block with onmouseover() but i can't click on it becasue as soon as I move my mouse away it disappears because of onmouseout()

暂无
暂无

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

相关问题 如何自动滚动div onmouseover并隐藏该div的滚动条? - How can I autoscroll a div onmouseover and hide the scrollbar for that div? 如何获取使用 onmouseover 在循环中创建的 div 的 ID - How can I get ID of div created in loop using onmouseover 如何停止滚动我的 <div> 的onmouseover? - How can I stop Scrolling my <div> onmouseover? 如何使用其类在导航上使用 onmouseover 事件? - How can I use the onmouseover event on a nav using its class? 我可以抓住元素 onmouseover 的 class 并在变量中使用它吗? - Can I grab the class of an element onmouseover and use that in a variable? onmouseover显示/隐藏div并可以选择div的文本 - onmouseover show/ hide div and can select the text of div IE中的select选项不能使用onmouseover事件 - can not use onmouseover event on select option in IE 当我将div悬停在另一个div中时,javascript会触发“ onmouseover”和“ onmouseout” - javascript triggers “onmouseover” and “onmouseout” when i hover the div in another div 如何减少onmouseover和onmouseoutt的使用? - How do I reduce the use of onmouseover and onmouseoutt? 做了一个<div> display:block with onmouseover() 但我无法点击它,因为一旦我将鼠标移开,它就会因为 onmouseout() 而消失</div><div id="text_translate"><p>所以我做了一个 display: block 当鼠标悬停在某个时,并 display: none 当 cursor 移开时。 '</p><p> <a href="https://i.stack.imgur.com/hu3Zz.png" rel="nofollow noreferrer">我制作的 div 仅在鼠标悬停在某个链接上时显示</a></p><p> <a href="https://i.stack.imgur.com/afbPC.png" rel="nofollow noreferrer">div 有一个 display: none 当鼠标离开链接时</a></p><p>这是我用过的代码</p><p> HTML:</p><pre> &lt;a href="#" onmouseover="LoginShow()" onmouseout="LoginHide()"&gt;Login/Sign Up&lt;/a&gt;</pre><p> JavaScript:</p><pre> function LoginShow (){ document.getElementById("log").style.display="block";} function LoginHide(){ document.getElementById("log").style.display="none";}</pre><p> 但是我无法单击 div,因为一旦我尝试将 cursor 移动到 div 中的按钮,则 div 将不显示任何内容,因为我必须将 cursor 远离链接。</p><p> 我是 JS 新手,但我见过其他 web 页面这样做,div 在鼠标悬停时显示的方式是什么,可以单击并显示:只有当我离开 div 时才显示。</p><p> 我也试过</p><pre> &lt;a href="#" onmouseover="LoginShow()"&gt;Login/Sign Up&lt;/a&gt; &lt;div class="login" id="log" onmouseover="LoginShow()" onmouseout="LoginHide()"&gt;</pre><p> It kind of solves the problem, but for the div to go to display none I have to move the cursor away from the div, if the move the cursor away from the anchor tag, it doesn't go away.</p></div> - made a <div> display:block with onmouseover() but i can't click on it becasue as soon as I move my mouse away it disappears because of onmouseout()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM