The if/else statement in my JavaScript code is malfunctioning, even though the individual code blocks have been tested successfully without the if/else

I have implemented an if/else statement to check the width of a div and adjust the iframe inside accordingly. The code itself runs without any issues, but I seem to be facing a problem with the if/else statement. You can view the site at

Code:

Javascript:

var sc = document.getElementById("scfullscreen");
var sccount = 0;
$(document).ready(function() {
    $(sc).click(function() {
        sccount= sccount+1;
        if (sccount%2 === 0) {
            animateFn('20%','80%');
            $('#soundcloud').animate({
                    left: '20%',
                    width: '80%',
                }, 1000);

        } else {
            animateFN('0%','20%');
        }
    });
});

function animateFn(l, w){
    $('#soundcloud').animate({
            left: l,
            width: w,
        }, 1000);
    $('#scfullscreen').animate({
            left: l,
            width: w,
        }, 1000);
    $('#scexpand').addClass('rotated');
}

HTML:

<!doctype html>
<html>

<head>
    <title>L3mmy Dubz</title>
    <link rel="stylesheet" type="text/css" href="styles.css">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="jquery.js" type="text/javascript"></script>
    <script src="animation.js" type="text/javascript"></script>
</head>

<body>
    <div id="header"></div>
    <a href="index.html">
        <div id="homebtn" class="btn">Home</div>
    </a>
    <div id="musicbtn" class="btn">Music</div>
    <iframe id="soundcloud" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/users/19690125&amp;auto_play=false&amp;hide_related=false&amp;show_comments=true&amp;show_user=true&amp;show_reposts=false&amp;visual=true"></iframe>
    <div class="btn" id="scfullscreen">
        <img id="scexpand" height="100%" alt="fullscreen" src="images\plus.png" />
    </div>

</body>

</html>

CSS:

* {
    margin:0px;
    border:0px;
    padding:0px;
}
body {
    background-color:#B22800;
    height:100%;
    width:100%;
}
#header {
    position:fixed;
    left:0px;
    width:20%;
    height:100%;
    background-color: #7C1C00;
    opacity:0.9;

}
.btn {
    position:fixed;
    line-height:200%;
    text-overflow:clip;
    display: block;
    overflow: hidden;
    white-space: nowrap;
    height:7.5%;
    width:20%;
    color:white;
    font-size:2em;
    text-align:center;
}
.btn:hover {
    background-color:#ff3a00;
}
#homebtn{
    top:0%;
}
#musicbtn{
    top:7.5%;
}
#header a {
    text-decoration:none;
    color:#fff;
}

#soundcloud {
    width:20%;
    height:77.5%;
    position:fixed;
    left:0px;
    top:15%;
}
#scfullscreen {
    bottom:0px;
    display:block;
    position:fixed;
    overflow: hidden;
    white-space: nowrap;
    height:7.5%;
    left:0px;
    width:20%;
}
#scfullscreen:hover {
    background-color:#ff3a00;
}
.rotated {
    transform: rotate(45deg);
    -ms-transform: rotate(45deg); /* IE 9 */
    -moz-transform: rotate(45deg); /* Firefox */
    -webkit-transform: rotate(45deg); /* Safari and Chrome */
    -o-transform: rotate(45deg); /* Opera */
}
@media screen and (max-width:768px) {
    #header {
        width:30%;
    }
    #soundcloud {
        width:30%;
    }
    #scfullscreen {
        width:30%;
        left:0px;
    }
}

Answer №1

Try optimizing your code for better performance.

var element = document.getElementById("elementID");
$(document).ready(function() {
$('#elementID').click(function() {
    var left = parseInt(element.style.left, 10);
    if (left > 0) {
        animateFunction('0%', '20%');
    } else {
        animateFunction('20%', '80%');
    }
});

function animateFunction(leftValue, widthValue) {
    $('#soundcloud').animate({
            left: leftValue,
            width: widthValue
        }, 1000);
    $('#elementID').animate({
            left: leftValue,
            width: widthValue,
        }, 1000);
    $('#expandButton').addClass('rotated');
}

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

Javascript editing enhancement for real-time changes

Are there any tools for in-place editing of Javascript code? I'm looking for something similar to Firebug, which is great for instant CSS editing and previewing but doesn't have the capability to edit JavaScript directly. Is there a tool or addon ...

A sporadic glitch in IE10 appears while attempting to translate text with a border-radius feature

I need some advice regarding a rendering issue I am experiencing in IE10. I have created an animated-flip effect that works perfectly in all the browsers I need it to. However, during testing, I noticed random border-like lines appearing for no apparent re ...

Is the chat box up and running properly? Am I doing this correctly?

As I venture into creating a chat box, I've come across some internet snippets that are quite complex to grasp. However, after much research, I have outlined a basic approach: Begin by creating a table in MySQL with columns for username, message, an ...

Django CSS not loading properly on webpage after initial load

I am having trouble understanding why the main.css file is not loading properly in my Django project. I am using the skeleton boilerplate and trying to change the background color, but the changes are not reflecting on the webpage. It's strange becaus ...

The function get_the_category() is not compatible when used within the <figcaption> element

I'm encountering a strange issue. I'm attempting to incorporate a javascript gallery from this site into my WordPress theme. My goal is to show the post category as a figcaption, but when I insert my PHP code inside, it doesn't function prop ...

What are the steps for integrating connect-multiparty into route paths?

I am looking to incorporate connect-multiparty into my routes. Upon researching, I found the following example... var multipart = require('connect-multiparty'); var multipartMiddleware = multipart(); app.post('/upload', multipartMiddle ...

Design your very own personalized Show Layout component

Currently, I'm in the process of creating a unique layout component to enhance the design of my Show page. I've encountered some inconsistencies with functionality, and my solution involves utilizing the Material-UI <Grid> component. While ...

Tabindex malfunction in Bootstrap dropdown menu when nested within a form and pressing Enter key

Within my form, I have integrated a Bootstrap 3 dropdown menu situated between two text input fields. The dropdown's role="menu" attribute allows for navigation using the up/down arrow and Enter keys. However, after making a selection in the dropdown ...

Attempting to resolve an error message with the help of JQuery

I need help figuring out how to clear an error icon when a user presses a key. ** EDIT - including the HTML code ** <input class="validate" type="text" data-type="string" id="address" /> <input class=" ...

The auto-play feature fails to function on iPhone devices when using Reactjs

I am currently working with Reactjs and Nextjs. I have a video on my website that is functioning properly on Android phones but not on iPhones. How can I resolve this issue? I have attempted the following code: <video loop autoplay='' muted> ...

CSS translation animation fails to execute successfully if the parent element is visible

Inquiries similar to this and this have been explored, but do not provide a solution for this particular scenario. The objective is to smoothly slide a menu onto the screen using CSS translation when its parent is displayed. However, the current issue is ...

Error encountered when initializing NextJS Firebase Authentication App

I'm encountering challenges with implementing Firebase authentication using Google Provider in NextJS. I have set up the necessary environment variables and successfully established a connection to Firebase. However, I'm running into an issue whe ...

If there is no setting variables, then set the correct variable

I'm having trouble understanding why this isn't functioning correctly. The variables are being set as follows, but the final result for "type" is 'm' instead of 'fm' cpanel_notifications - 1 remote_server - 1 if ($_POST[&a ...

How to Achieve Seamless Vertical Scrolling Through Clicking a Button

I'm currently working with Bootstrap 4 Modal and have removed the Overflow-Y (ScrollBar) to enhance the design. However, I've also incorporated a function with two buttons (Up & Down) using JavaScript to facilitate easier navigation for users. Th ...

What is the best method for restricting the visibility of an animation to specific regions within an SVG?

I am seeking assistance with adding a "shine" animation to my SVG. I want the animation to only appear within specific parts of the SVG, but I'm uncertain about how to achieve this. Below is what I have accomplished so far. The aim is for the white ...

Compiling TypeScript on save disrupts AngularJS functionality in Visual Studio 2017

I am currently working on a project in Visual Studio Community 2017 (15.2) that involves AngularJS 1.6.5 and a NancyFX server. You can find the code for this project here: https://github.com/GusBeare/NancyAngularTests This project serves as a learning pl ...

Does the height of a block element change based on the font size?

Does the font size of content affect the height of a block element? Let me demonstrate what I'm talking about, take a look at this example fiddle If you enlarge the font size of the class .p within the div, you'll notice that the div's hei ...

connecting elements in an object's array to another array within a nested object

Is there a way to iterate through each string value in the imageKeys array and use it to create a URL for each object in the images array within the nested ImageList object, all without changing the original object? const myInitialStateFromParent = { ...

Showcasing various images simultaneously within a table cell with the power of JavaScript

For my game project, I need to create an 8x8 table where multiple coins are displayed simultaneously for 3000 milliseconds at random positions. When the "Start" button is clicked, the coin display should continue for 1 minute. However, I am facing an issue ...

Solve inconsistent divs using HTML and Bootstrap styling techniques

I'm currently working on a .cshtml page where I display database objects in a card format using HTML and Bootstrap. However, the cards are appearing unevenly as shown below: https://i.sstatic.net/LbrrH.png Here is the code snippet I am using: @model ...