jQuery 'if' condition not getting detected

My jQuery code is not recognizing an if statement. You can check out the CodePen link here.

On my site, I have 4 page transitions - page right, page left, page up, and page down. The contact page is hidden beneath the main hero sections and appears with the up and down transitions. I want the contact page to slide back down if the user has it open and clicks on page right or left. I used an if statement to check the marginTop value of the contact div, but it's not working as expected.

You can view the code snippet below and also access the full code on the provided CodePen link.

$(document).ready(function() {

  // The if statement is not working as expected

  if ($('.contact-wrapper').css("marginTop") == '-10%') {
    $('#down').trigger('click');

  }
  // --------------------------------------------

  $('#pg-right').click(function() {
    $('.home-wrapper, .about-wrapper').animate({
      marginLeft: '-=100%'
    }, 500);
  });

  $('#pg-left').click(function() {
    $('.home-wrapper, .about-wrapper').animate({
      marginLeft: '+=100%'
    }, 500);
  });

  $('#up').click(function() {
    $('.contact-wrapper').animate({
      marginTop: '-=10%'
    }, 500);
  });

  $('#down').click(function() {
    $('.contact-wrapper').animate({
      marginTop: '+=10%'
    }, 500);
  });
})
    .home-wrapper {

      position: fixed;

      background: blue;

      height: 90vh;

      left: 0;

      width: 100%;

    }

    .about-wrapper {

      position: fixed;

      background: green;

      height: 90vh;

      width: 100%;

      left: 100%;

    }

    .contact-wrapper {

      position: fixed;

      background: black;

      height: 80vh;

      width: 100%;

      left: 0;

      top: 90%;

    }

    #pg-right,

    #pg-left,

    #up,

    #down {

      position: fixed;

      font-size: 3em;

      color: white;

    }

    #pg-right {

      top: 10%;

      left: 80%;

    }

    #pg-left {

      top: 10%;

      left: 20%;

    }

    #up {

      top: 50%;

      left: 60%;

    }

    #down {

      top: 50%;

      left: 40%;

    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div class="home-wrapper"></div>

<div class="about-wrapper"></div>

<div class="contact-wrapper"></div>
<a href="#" id="pg-right">Right</a>
<a href="#" id="pg-left">Left</a>
<a href="#" id="up">Up</a>
<a href="#" id="down">Down</a>

Answer №1

Big shoutout to @Xufox for guiding me in the right direction! The issue I was facing stemmed from the fact that my jQuery if statement was evaluating in pixels, while I needed it in %. To rectify this problem, I converted the string into an Integer value and then checked if it was less than zero. Take a look at the code snippet below or visit the updated version on CodePen.

var x = $('.contact-wrapper').css("marginTop")

if( parseInt(x) < 0) {
  $('#down').trigger('click');
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Implementing a class change to anchor elements when rearranging them

I have 4 divs and their corresponding anchor tags. The active class is dynamically added to anchors on scroll unless the order of the display elements changes. In that case, active classes are only applied to the sorted elements. I also sorted the nav anch ...

Swapping IMG depending on breakpoint in Material-UI

I understand how to adjust styling with breakpoints for various properties like height, width, and font size. However, I am struggling to find examples of switching images based on screen sizes. My goal is to replace an image with a different one dependin ...

Struggling with React integration of AdminLTE3 sidebar treeview?

I have a requirement to create a React sidebar with a category 'Staff' that, when clicked, reveals three subordinate categories. Below is the code snippet: import React, { Component } from "react"; export default class Sidebar extends Componen ...

What is the best method for parsing information from the HTML source code of a specific tab on a webpage?

I have tried various methods mentioned in different responses, such as testing with different user agents (Chrome, Safari etc), and fetching HTML directly using HTTPClient and BufferedReader, but unfortunately none of them seem to be effective. How can I ...

Customize URL based on selected button

My question may be a bit unclear, but I want to generate dynamic URLs that redirect users to specific pages based on the link clicked. For example: Parent page with links (a, b, c, x, y) User clicks on 'b' User is taken to a Node Page that play ...

What strategies can I use to divide lengthy words in my Django application?

My username on the site is causing overflow outside of the content box, as shown here I tried adding h1, h2, h3, h4, h5, h6 { color: #44444; overflow-wrap: break-word;}, but it didn't work. Here is the code for the profile page: All CSS styles for ...

Issue with Async pipe when utilizing autocomplete functionality

HTML Code <mat-form-field> <input type="text" matInput class="formControl" [formControl]="name" [matAutocomplete]="auto" > <mat-autocomplete #auto="matAutocomplete"> <mat-option *ngFor="let option of city | async" [valu ...

Symfony: Enhancing array elements with updated bootstrap progress bar

I'm facing a challenging issue that I need help with. Currently, I have a command set up to send a large number of emails (around 300 or more) every 5 minutes using cron in order to distribute a newsletter. My goal is to keep track of how many emails ...

Trouble with X-editable: Unable to view values when editing and setting values using J

When using X-editable to modify a form with data, I encounter an issue. Initially, the values are loaded from the database to the HTML, but when users try to edit by clicking on the "Edit" button, all values appear as "Empty" instead of their actual cont ...

Implementing personalized SVG icons for list items

Here is some HTML code along with SCSS that I need help with: <div class="risk"> <ul> <li><span>Aggressive</span>Lorem ipsum dolor ...</li> <li><span>Growth</span>doloremque autem...</li ...

Adding information to an HTML table using JQuery

Is there a way to incorporate JSON data into an HTML table? The JSON data follows this format: https://i.stack.imgur.com/lzdZg.png This is the desired HTML table structure: <table class="table table-bordered table-hover "> ...

What is the best way to adjust the height and width of a div when it comes into view through scrolling?

How can I automatically and frequently change the size of my div when it is scrolled into view? HTML <div id="contact" class="fadeInBlock"> <div class="map"> <div class="pointer"> <div class="dot"></div& ...

Every time a new message is sent or received, I am automatically brought back to the top of the screen on the

I'm currently working on integrating a chat feature into my Angular Firestore and Firebase app. Everything seems to be functioning well, except for one issue - whenever a new message is sent or received, the screen automatically scrolls up and gets st ...

CSS code to create a single content bar flanked by two sidebars

I have been working on a beginner-friendly webpage and have managed to style it using CSS to some extent. My goal is to have the content centered with a white background, flanked by two sidebars styled in blue. To work on this page, please visit: The ...

Is there a way to connect a CSS external file that is saved on Dropbox?

Is it possible to link an external CSS file stored in Dropbox with HTML? I have followed all the instructions I could find online, including clicking and pressing "Copy Dropbox Link" to generate an href link. However, it doesn't seem to be working. ...

"Choose between displaying the Bootstrap Column consistently in a vertical orientation or a horizontal orientation

I'm just diving into the world of HTML/CSS and currently experimenting with Bootstrap Studio for a project. My focus is on creating an 'About' section for a webpage, where I've structured four rows each containing two columns as follows ...

What could be causing the bootstrap 4 col-md-3 block to shrink when using position fixed?

When working with Bootstrap 4, I encountered an issue where changing the block position from relative to fixed using a script caused the block to decrease in size. The script I used includes a .sticky class: .sticky { position: fixed !important; top: ...

Troubles with implementing child routes in Angular 6

I'm having trouble getting the routing and child routing to work in my simple navigation for an angular 6 app. I've configured everything correctly, but it just doesn't seem to be working. Here is the structure of my app: └───src ...

What is the best way to keep calling an AJAX function until it receives a response from a previous AJAX call

I am looking to continuously call my ajax function until the previous ajax call receives a response. Currently, the page is loading every second and triggering the ajax function, but I want it to keep calling the ajax function until the previous one has c ...

Two conflicting jQuery plugins are causing issues

In my journey to learn jQuery, I encountered an issue while working on a website that utilizes a scroll function for navigating between pages. The scripts used in this functionality are as follows: <script type="text/javascript" src="js/jquery-1.3.1.mi ...