javascript - How to keep my drop-down menu properly aligned with my element? -
i created drop-down menu appears when click on text contains class trigger allows open menu, have problem. page responsive, need menu correctly aligned element: http://prntscr.com/7gw5ox
when resize page: http://prntscr.com/7gw5wd
my html code (down-down menu - placed @ bottom of page):
<div id="help-down-down-menu" class="drop-down-menu"> <ul> <li> <a href="faq.php">frequently asked questions</a> </li> <li> <a href="faq.php">test</a> </li> <li> <a href="faq.php">test</a> </li> <li> <a href="faq.php">test</a> </li> </ul> </div>
footer code text contains drop-down menu trigger:
<div id="footer"> <div class="wrapper"> <ul> <li> <span class="drop-down-menu-trigger" id="help">help</span> </li> </ul> <span id="footer-copyright"> <a href="./..">coded dylan - ©2015-2016</a> </span> </div> </div>
javascript code:
(function($) { $(".drop-down-menu-trigger").click(function(e) { e.preventdefault(); $(".drop-down-menu").css({"visibility": "visible"}); }); })(jquery);
css:
.drop-down-menu { background-color: #ffffff; box-shadow: 0 15px 110px rgba(0, 0, 0, 0.2); border-radius: 3px; text-align: center; position: absolute; top: 150%; left: 500px; visibility: hidden; } .drop-down-menu:after { top: 100%; left: 50%; margin-left: -15px; border: 15px solid transparent; content: " "; height: 0; width: 0; position: absolute; pointer-events: none; border-top-color: #ffffff; } .drop-down-menu { display: block; color: #000000; padding: 10px; } .drop-down-menu a:hover { background-color: #f5f5f5; } .drop-down-menu #faq:before { content: "\f059"; font-size: 18px; margin-right: 5px; }
you need modify code this:
html : after footer comments html code must this
<div class="drop-display"> <div id="help-down-down-menu" class="drop-down-menu"> <ul> <li> <a class="modal-window-trigger" name="modal-window-faq" id="faq" href="faq.php">frequently asked questions</a> </li> <li> <a href="faq.php">test</a> </li> <li> <a href="faq.php">test</a> </li> <li> <a href="faq.php">test</a> </li> </ul> </div> </div> <div id="footer"> <div class="wrapper"> <ul> <!-- +++++++++++++++++++++++++++++++++ added & updated +++++++++++++++++++++++++++++ --> <li> <span class="drop-down-menu-trigger" id="help" onclick="getpos(this)">help</span> </li> <li> <span class="drop-down-menu-trigger" id="test" onclick="getpos(this)">help</span> </li> <!-- +++++++++++++++++++++++++++++++++ added & updated +++++++++++++++++++++++++++++ --> </ul> <span id="footer-copyright"> <a href="./..">coded dylan - ©2015-2016</a> </span> </div> </div> <div id="modal-window-faq" class="modal-window"> ... ... ... <!-- same code is--> </div> <div id="expose-mask"></div> <script src="javascript/scripts.js"></script> </div> </div> </body> </html>
css
1) added class: drop-display,
2) removed 1 drop-down-menu class repeating, , modified one.
.drop-display { display: block; width: 100%; //text-align: center; remove line position: absolute; /* changed absolute */ z-index: 999; } .drop-down-menu // no update here { display:inline-block; background-color: #ffffff; box-shadow: 0 15px 110px rgba(0, 0, 0, 0.2); border-radius: 3px; text-align: center; position: relative; }
jquery
1) added line hide drop-down-menu on page load
2) modified code of visibility fadein() or fadetoggle()
$(".drop-down-menu").hide(); // on first line of scripts.js file function getpos(elems) { var x = elems.offsetleft, y = elems.offsettop; var curid = event.target.id; var hw = $("#" + curid ).width(); var dw = $(".drop-down-menu").width(); var dh = $(".drop-down-menu").height(); var temp = dw/2; var tempx = hw/2; var xpos = x - temp + tempx; var ypos = y - dh - 20; $(".drop-display").css("left", xpos + "px"); $(".drop-display").css("top", ypos + "px"); $(".drop-down-menu").fadetoggle(); }
i sure work, if still issue feel free ask.
update :
html - added 1 more ul , onclick event
css - removed text-align:center line , changed position relative absolute.
jquery - total change
Comments
Post a Comment