Adjusting the CSS class name based on the screen size and applying styles to that class only during the "onload" event

The website I am currently developing can be found at . It is built on WordPress using the Avada theme and everything seems to be functioning correctly. The image move up effect on this site is achieved with the following JavaScript:

window.onload = function() {
    var size = -500;
    document.getElementById("sliders-container").style.marginTop = size + "px";
};

Additionally, the CSS used for this effect is as follows:

#sliders-container{
    transition: 5s;
}

However, upon testing the site in various responsive layouts, I noticed that the image moves up too much and eventually becomes invisible. My goal is to ensure that the image remains visible to some extent instead of completely disappearing. To address this issue, I attempted to adjust the JavaScript code based on window width:

window.onload = function() {   
    var windowWidth = $(window).width();
    if(windowWidth > 768){
        var size = -500;
    }
    document.getElementById("sliders-container").style.marginTop = size + "px";
};

Despite my efforts, this modified script did not produce the desired results. I am relatively new to JavaScript and eager to expand my knowledge in this area.

Answer №1

Implement media queries within your CSS stylesheet.

Consider the following example:

<style>
@media screen and (max-width: 500px) {
body {
    background-color: lightblue;
}
}
</style>

Answer №2

I implemented my desired functionality using the following JavaScript code:

window.onload = function()
    {
        if ( jQuery(window).width() > 1280) {      
            document.getElementById('sliders-container').className = 'transition2';
        }
        else if ( jQuery(window).width() > 600 && jQuery(window).width() < 1280) {
            document.getElementById('sliders-container').className = 'transition3';
        }
        else if ( jQuery(window).width() < 600) {
            document.getElementById('sliders-container').className = 'transition4';
        }
        var size = -500;
        var elems = document.getElementsByClassName('transition2');
        for(var i = 0; i < elems.length; i++) {
        elems[i].style.marginTop = size + "px";
        }
        var size = -360;
        var elems = document.getElementsByClassName('transition3');
        for(var i = 0; i < elems.length; i++) {
        elems[i].style.marginTop = size + "px";
        }
        var size = -200;
        var elems = document.getElementsByClassName('transition4');
        for(var i = 0; i < elems.length; i++) {
        elems[i].style.marginTop = size + "px";
        }
    };

Credits to @CBroe for the inspiration.

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

What is the best way to combine a JSON response object with the variable J inside a for loop?

Received a JSON response as follows: { "name" : "chanchal", "login3" : "1534165718", "login7" : "1534168971", "login6" : "1534168506", "login5" : "1534166215", "login9" : "1534170027", "login2" : "1534148039", "lastname" : "khandelwal", ...

Why won't both routes for Sequelize model querying work simultaneously?

Currently, I am experimenting with different routes in Express while utilizing Sequelize to create my models. I have established two models that function independently of one another. However, I am aiming to have them both operational simultaneously. A sea ...

Utilizing dynamic content to pass arguments to JavaScript functions

I'm facing a challenging issue that is causing me frustration. While browsing through this platform, I found some potential solutions but encountered a roadblock in implementing them successfully. My current project involves developing an e-commerce p ...

Retrieving user input in React by utilizing the onChange event handler

I have been tasked with creating a Quiz App and I am currently working on building it. The app consists of several components such as "question", "question-list", and "add-question". Within the "add-question" component, there is a form that allows users ...

What is the best way to handle AJAX responses in an onchange event

I'm working on a form where the select option in the input (jurusan) is automatically filled based on the selected value. I need to know how to change the value that appears after selecting an option. For example, if I select option A, it displays th ...

Display a hidden div on hover using JQUERY

How can I make a hover popup appear when submitting a form, and have it disappear only when the mouse is out of both the popup div and the submit button? Currently, the hover popup shows up but disappears when entering the popup. Can someone assist me in r ...

Navigating to the left while implementing right-to-left formatting on a single webpage

I'm currently working on a website and I decided to make it right-to-left (RTL). Everything looks good on the website, except for one specific page. I'm experiencing some difficulties in implementing RTL on this particular page. Although the ot ...

Encountered a problem when attempting to upload images to Cloudinary through the front-end and save the information in a MySQL database using Axios on Node JS

I am currently working on a web application using React. I have implemented a form where users can input text data and upload multiple image files. The goal is to store the submitted images on Cloudinary along with other text data in a MySQL database. Unf ...

Issue with bootstrap accordion not collapsing other open tabs automatically

I'm struggling to incorporate a Bootstrap accordion collapse feature into my project, specifically with the auto-collapsing functionality. In the scenario where there are 3 divs labeled A, B, and C, if A is open and I click on B, A should automaticall ...

problem encountered when converting SVG HTML into SVG paths

Recently, I came across an interesting SVG file depicting a question mark enclosed in a circle. Intrigued by its design, I wanted to extract the paths from this SVG image so that I could integrate it directly into my codebase. Following some online tutoria ...

Passport sessions do not retain persistence

Having some trouble implementing OAuth 2.0 login where the sessions don't persist after authentication is a common issue. Additionally, there seems to be a problem with the app getting stuck in the routes/bnetauth.js file during the redirect in the ca ...

Google Chart Fails to Display

I am encountering an issue while attempting to integrate a Google chart into my project. The page fails to load, rendering a blank screen. Initially, the page displays correctly for a brief moment after loading and then suddenly turns blank, becoming unres ...

What is the correct way to invoke a function contained within an object that is stored in an array?

I've encountered a problem with my program. I'm attempting to invoke a function that is part of an object stored in an array, but I'm having difficulty calling the function correctly. //Initialize Array for Storing Projects let allProjects ...

Stop the inclusion of the scrollbar in the mat-drawer-inner-container within the Angular mat-drawer Component

Background Story: Working on designing a screen layout that includes the use of a mat-drawer to display a custom component. The challenge arises when the custom component gets nested inside a div (with class="mat-drawer-inner-container") automatically adde ...

A guide on pulling all orders simultaneously in WooCommerce through their REST API

I'm currently utilizing the WooCommerce REST API PHP Client Library to oversee an external order system. Below is the code snippet I am working with: <?php require_once( 'lib/woocommerce-api.php' ); $options = array( 'debug&ap ...

What steps can I take to enhance the efficiency of this JavaScript DOM data manipulation algorithm?

Purpose I am working with a DOM that contains around 70 elements (divs with content). I have the need to move and toggle the display of these divs frequently and rapidly. Speed is crucial in this process. The trigger for moving and toggling these divs is ...

Utilizing Electron and jQuery to incorporate a loading animated GIF into a newly opened window, where the animation pauses

I am working on an electron project that involves opening a new window with an index.html file. In this newly opened window, I have included an animated.gif in the body section. <!doctype html> <html lang="en> <head> <meta charset ...

What is the best way to save a JavaScript variable in local storage?

I am facing an issue with a JavaScript variable named addNumber. This variable is responsible for adding 1 to a div every time a button is clicked. However, the problem arises when I navigate between different pages on my website - the counter resets back ...

Utilizing Nicknames in a JavaScript Function

I'm dealing with a function that is responsible for constructing URLs using relative paths like ../../assets/images/content/recipe/. My goal is to replace the ../../assets/images section with a Vite alias, but I'm facing some challenges. Let me ...

JavaScript Promise Fundamentals

While I am quite familiar with coding in JavaScript, the benefits of promises in the JS world still seem somewhat unclear to me. Below is an example of asynchronous calls using callbacks nested within each other. (function doWorkOldSchool() { setTime ...