Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript/jQuery hover on separate elements
    primarykey
    data
    text
    <p>I have a strange problem, stemming from a layout that I cannot change to better solve this.</p> <p>Basically I have a menu like so:</p> <pre><code>&lt;div id="hornav"&gt; &lt;ul class="container"&gt; &lt;li class="item1"&gt;link&lt;/li&gt; &lt;li class="item2"&gt;link&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; </code></pre> <p>And I have drop downs separated like so:</p> <pre><code> &lt;div class="dropdowns"&gt; &lt;div id="ditem1" class="dropdown-div"&gt;content&lt;/div&gt; &lt;div id="ditem2" class="dropdown-div"&gt;content&lt;/div&gt; &lt;/div&gt; </code></pre> <p>What i need to do is make the links hover to show the container. I can do this but I also need to make it so if I move my mouse over the drop down that shows, it does not disappear.</p> <p><em>Because of the way the site is running, and what tools I am limited by I <strong>cannot</strong> make the dropdowns inside of the li elements (It is dynamically generated by the CMS, without any options)</em> - This point is extremely important.</p> <p>Right now this is the javascript code i am using. It functions to an extent, though IT is very buggy (If i hover over the contained element, then hover back it vanishes). This code may be slightly outdated as I have been gradually trying multiple methods, and reading up on this problem with little success.</p> <pre><code>function dropdown(event,passDown){ var classes=$(passDown).attr('class').split(' '); for(var i=0;i&lt;classes.length;i++){ if(classes[i].indexOf('item')!=-1){ var classId=classes[i]; } } var elem=$('#d'+classId); event.preventDefault(); if(!elem.hasClass('active')){ $('#hornav li.active,.dropdown-div.active').each(function(){ $(this).removeClass('active'); }); $('#d'+classId).addClass('active'); $(passDown).parent().addClass('active'); }else{ $('#hornav li.active,.dropdown-div.active').each(function(){ $(this).removeClass('active'); }); } } $(document).ready(function(){ $('#hornav&gt;ul&gt;li[class*="item"]:not(.item20)').each(function(){ //trigger all drop down links $(this).hover(function(event){ event.stopPropagation(); console.log(event); var setIt=this; if(event.relatedTarget.id.indexOf('ditem')==-1){ dropdown(event,this); } }); }); $('.dropdowns .dropdown-div').each(function(){ $(this).hover(function(event){ event.stopPropagation(); console.log(event); var setIt=this; //if(event.offsetParent.className.indexOf('item')==-1){ $('#hornav li.active,.dropdown-div.active').each(function(){ $(this).removeClass('active'); }); //} }); }); }); </code></pre> <p>EDIT:</p> <p>We have decided to take another approach and are going to use clicks instead of hovers witch will not cause the problem.</p> <p>I will leave this open for now, as it seems like a question that could help others out.</p> <p>EDIT 2: </p> <p>Never solved this and ended up coming up with another completely different solution. However I feel this question may help people in the future so I will leave it open if anyone wants to answer it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload