0

I'm trying to convert a site (originally html/css) into a WordPress theme, and a lot if not most, is going to be hard code still.

I've added a menu that is responsive to the website, however, can't seem to get it to work to my needs.

I'm wanting it to have a little hamburger icon, or even just "Menu" that they can click on, and will open the menu of choices.

Right now, this is what it looks like normally.

This is what it looks like "closed".

It's just a little white bar when it's closed, and when you click to open it, it's actually a link for the first list..

Here's the HTML for the menu.

<nav class="nav">
    <ul>
        <li><a href="">Home</a></li>
        <li><a href="#">Artists</a></li>
        <li><a href="#">Framing</a></li>
        <li><a href="#">Restorations</a></li>
        <li><a href="#">Contact</a></li>
    </ul>
</nav>

The CSS is a bit more complex and long, as it also has the @media queries in it.

.nav-container {
    margin:0 auto;
    max-width:1000px;
    background:#333;
    clear:both
}

.nav-inner {
    max-width:658px;
    margin:0 auto
}

nav.main {
    margin-left:-34px
}

nav.main ul {
    border-right:#FFF solid 2px
}

nav.main ul li {
    display:inline-block;
    min-height:29px;
    padding:17px 40px 5px;
    text-align:center;
    background-color:#7d833d;
    border-left:#FFF solid 2px;
    margin-left:-4px;
    font-family:Gotham,"Helvetica Neue",Helvetica,Arial,sans-serif;
    font-weight:700;
    font-size:1em
}

nav.main a {
    color:#FFF;
    text-decoration:none
}

nav.main a:hover {
    color:#000;
    text-shadow:none
}

nav.main a:visited {
}

/* nav */
.nav {
    position:relative;
    margin:20px 0;
/*height: 60px;*/
    line-height:5;
    padding-top:3px
}

.nav ul {
    margin:0;
    padding:0;
    margin-top:-10px
}

.nav li {
/*margin: 0 5px 10px 0;*/
    padding:0;
    list-style:none;
    display:inline-block;
    min-height:29px;
/*padding: 17px 40px 7px 40px;*/
    text-align:center;
    background-color:#7d833d;
    border-left:#FFF solid 2px;
    margin-left:-8px;
    font-family:Gotham,"Helvetica Neue",Helvetica,Arial,sans-serif;
    font-weight:700;
    font-size:1em;
    padding-left:3px;
    padding-right:3px;
    color:#fff
}

.nav a {
    padding:3px 12px;
    text-decoration:none;
    color:#fff;
    line-height:100%
}

.nav a:hover {
    color:#000
}

.nav .current a {
    background:#999;
    color:#fff;
    border-radius:5px
}

/* center nav */
.nav.center ul {
    text-align:center
}

@media screen and (max-width: 600px) {
.nav {
    position:relative;
    min-height:40px
}

.nav ul {
    width:100%;
    padding:2px 0;
    position:absolute;
    top:0;
    left:0;
    border:solid 1px #aaa;
    background:#fff url(images/icon-menu.png) no-repeat 10px 11px;
    border-radius:5px;
    box-shadow:0 1px 2px rgba(0,0,0,.3);
/* margin-top: 5px; */
    height:auto;
    padding-top:3px;
    padding-bottom:5px
}

.nav li {
    display:none;
/* hide all <li> items */
    margin:0
}

.nav .current {
    display:block
/* show only current <li> item */
}

.nav a {
    display:block;
    padding:5px 5px 5px 32px;
    text-align:left
}

.nav .current a {
    background:none;
    color:#666
}

/* on nav hover */
.nav ul:hover {
    background-image:none
}

.nav ul:hover li {
    display:block;
    margin:0 0 5px
}

.nav ul:hover .current {
    background:url(images/icon-check.png) no-repeat 10px 7px
}

/* right nav */
.nav.right ul {
    left:auto;
    right:0
}

/* center nav */
.nav.center ul {
    left:50%;
    margin-left:-90px
}

.address-box {
    margin-bottom:15px
}

I've looked through a few pages of Google, but it's harder to find an answer with a specific menu. I can't get JQuery to work with WordPress, so HTML/CSS is preferred.

Bruno Ribeiro
  • 5,956
  • 5
  • 39
  • 48
  • 3
    Aside from radio buttons, there are really no click events in CSS. The right solution is to learn what's causing your jQuery problem. With a username like yours, I'd expect no less. – isherwood Apr 15 '15 at 21:09
  • I've looked unto it, just haven't gotten the WordPress system down to a t. I've been working with Drupal, making themes from scratch, however, a customer wanted this site moved from a Drupal to WordPress.. I'm sure there's a simple thing I'm missing. Would you think it to be possible for a radio button to act as a toggle with a responsive menu? – Webmasters Apr 15 '15 at 21:14
  • Here's a start. Notice how I'm using `jQuery` for the document.ready function, but passing in the `$` alias so I can use it in my functions. http://liveweave.com/Rj1RF1 – isherwood Apr 15 '15 at 21:14

4 Answers4

0

There's a trick you can use with :focus and tabindex attribute to make any element focusable. This allows the "toggle" like property to be made. See the example in the snippet below:

.submenu { display: none; }

/* [tabindex] can be used to use :focus on normally unfocusable elements */
li:focus > .submenu {
  display: block;
  margin-left: 10px;
}

/* Remove outlines from focus */
li[tabindex] { outline: none; }
<ul>
    <li tabindex="">Click me!
        <ul class='submenu'>
            <li>Submenu</li>
            <li>Submenu</li>
            <li>Submenu</li>
        </ul>
    </li>
</ul>

See this JSFiddle for a more advanced example.

Reference: https://stackoverflow.com/a/8616525/1113225

Community
  • 1
  • 1
s4nji
  • 449
  • 1
  • 6
  • 17
  • I understand the concept, but when viewing the JSFiddle, it doesn't work? Is it not supposed to display the menu upon clicking? If not, could you explain exactly what the focus is supposed to do? – Webmasters Apr 16 '15 at 19:11
0

I always find that using a good framework helps solve this problem quickly. I like using one called UIKit. I'm also a fan of bootstrap, but have liked uikit a bit more for some of its features like the 'off canvas' navigation feature, found here: off canvas navigation - uikit

I usually use media queries to handle the visibility of the 'hamburger button' that I use for opening the off canvas nav. (Uikit also has that hamburger nav icon built in) it's pretty easy to set up.

Depending on when you want your hamburger button to show up, your media query could be something like this:

@media all and (max-width:767px){ .hamburger-btn {display:block;} }
@media all and (min-width:768px){ .hamburger-btn {display:none;} }
Gordon Smith
  • 124
  • 13
  • Yeah, I believe I'm going to be using a framework next time. This time, we had to convert a Drupal theme to HTML/CSS.. Then we had to configure it for WordPress, so it's really not meant to be in WordPress, but we're making it work. You ever used _s? – Webmasters Apr 16 '15 at 19:10
  • @Webmasters I'm looking at _s right now. It looks like a good starter template. – Gordon Smith Apr 16 '15 at 20:06
  • Alright let me know what your thoughts are, if I can find a good basic template, that isn't pre-designed so to speak, that'd be ideal, so I can add plugins etc through WordPress without hard coding, and running into issues like this one. – Webmasters Apr 16 '15 at 20:59
  • Well, in looking at _s, the theme already seems to have a built in mobile menu. It's not like the offcanvas menu that uikit offers, but it really does seem to be close to what you're trying to do and its already done for you just in the core build of _s. I think you're off to a good start. I noticed that once you collapse the page under 600px, the mobile menu button shows up and it toggles the mobile version of the navigation. Sure, you're going to have to do some styling of course, but I'd say its a great place to start. Just add your css / html and you're set. – Gordon Smith Apr 16 '15 at 21:27
  • Yeah I haven't started using _s yet, but plan to in the future. Because I'm going to have to start building more complex e-commerce websites in WordPress and won't be using a theme, but a framework. Still trying to find something in regards to working with the mobile menu, without JS however. – Webmasters Apr 20 '15 at 17:23
  • I have used the nav walker class to tie in the wordpress built in navigation into my sites, it seems to work well. https://github.com/twittem/wp-bootstrap-navwalker – Gordon Smith Apr 21 '15 at 18:24
-1

jQuery more than likely isn't working because of noconflict. You'll need to wrap your jquery code in this:

jQuery(document).ready(function ($) {
  //Your code here
});

You could also write your jQuery using jQuery instead of $.

After that, using $ won't cause you any more problems.

Here's a simple navigation close example using jQuery:

http://jsfiddle.net/austinthedeveloper/52vxsun1/

jQuery(document).ready(function ($) {
    $('h1').on('click',function(){
        $('nav').slideToggle();
    });
});
austinthedeveloper
  • 2,401
  • 19
  • 27
-2

I do not think that it is possible to make a menu that is as compatible as using jquery but one thing you can try is the :hover feature in css. Here is an example I made, try manipulating it into your code to make it do what you want using the opacity attribute.

<div>
    <h2>Hover Here</h2>
    <ul>
        <li>List Item1</li>
        <li>List Item2</li>
        <li>List Item3</li>
    </ul>
</div>
div {
    height:130px;
    width:150px;
    background-color:yellow;
}
ul {
    opacity:0;
}
ul:hover {
    opacity:1
}

http://jsfiddle.net/JoshuaHurlburt/avttkdst/

Joshua
  • 822
  • 11
  • 24
  • Looking at your code, I'm not even seeing use of the hover feature with anything like that.. As well, I'm wanting it to when you click it, it toggles a drop down (the menu). Not when you hover over it, that's almost the same problem I'm having right now. Thanks for trying! – Webmasters Apr 15 '15 at 21:24