-1

I'm trying to hide #back when #mobile-nav is positioned 0vw from the left. Here's what I have at the moment...

$(document).ready(function() { 
  var position = $("#mobile-nav").css("left", "0vw");
  if (position < 0vw) {
    $("#back").css("display", "none");
  } else {
    $("#back").css("display", "block");
  }
);

Any idea as to why this isn't returning successfully? Here's a link to the codepen https://codepen.io/spittman/pen/pBqYON

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Sam
  • 13
  • 4
  • Open console in the browser and you will see why ;) your code is failing on this line `if (position < 0vw) {`, change it to `if (position < 0) {`. Also closing `ready` function should be `});` – Morpheus May 01 '19 at 08:59
  • 1
    Firstly you're using the setter of `css()` so `position` will hold a jQuery object, not a numerical value. Secondly, even if it did hold a value you could use to test the position, `position < 0vw` is syntactically incorrect – Rory McCrossan May 01 '19 at 09:03
  • Possible duplicate of [Hide div if screen is smaller than a certain width](https://stackoverflow.com/questions/4296012/hide-div-if-screen-is-smaller-than-a-certain-width) – xmaster May 01 '19 at 09:03
  • @RoryMcCrossan thanks for your help. I'm a bit of a beginner with javascript and jquery so excuse my naivety. here's what I now have, unfortunately it's still not working https://codepen.io/spittman/pen/pBqYON – Sam May 01 '19 at 09:23

2 Answers2

0

Try this:

var position = parseInt($("#mobile-nav").css("left"));

    if (position <= 0) {
        $("#back").css("display", "none");
    } 
    else {
        $("#back").css("display", "block");
    }
Aneta
  • 78
  • 7
0

try this

$("ul ul").hide();


$("li").click(function(event) {
  $(this).children('ul').toggle();
  event.stopPropagation();
});

$("li").click(function() {
  $(this).siblings().children().hide();
});

$(".nav-child-trigger").click(function(){
  $("#mobile-nav").animate({left: '-=100vw'});
});

$("#back").click(function(){
  $("#mobile-nav").animate({left: '+=100vw'});
  var position = $("#mobile-nav").position().left;

    if (position > 0) {
        $("#back").css("display", "none");
    } 
    else {
        $("#back").css("display", "block");
    }
});


$(document).ready(function(){ 


});
A J
  • 441
  • 4
  • 20