Struggling with getting the JavaScript, scss, and CSS television animation to turn on and off properly? Seeking assistance to troubleshoot this

After finding this javascript code on Codepen and seeing that it worked perfectly in the console there, I tried to run it on my computer with jQuery but it didn't work outside of Codepen. Even when attempting to use it on JSfiddle or compile the SCSS code into CSS, it still wouldn't function properly. Surprisingly, the code only works flawlessly within Codepen. The button effectively turns the TV on and off.

Check out the codepen link here.

Here is the SCSS JSfiddle for reference.

And here is the CSS JSfiddle link.

Below is the HTML code snippet:

<section class="screen">
  <div class="content">
    <!-- [FS] NOTE: This can also function as a normal web page with regular content -->
  </div>
</section>
<button id="switcher-tv">Turn on/off</button>

Here is the SCSS code:

$color-text: #e1eef6;
$color-link: #ff5f2e;
$color-link-hover: #fcbe32;
$black: #111111;

body {
  font-family: sans-serif;
  background-color: $black;
}

a {
  color: $color-link;
  
  &:hover {
    color: $color-link-hover;
  }
}

button {
  position: fixed;
  right: 20px;
  bottom: 20px;
  padding: 20px;
  font-weight: 700;
  font-size: 16px;
}

h1 {
  text-align: center;
}

.screen {
  background-color: $color-text;
  position: fixed;
  top: 50%;
  left: 50%;
  width: 100vw;
  height: 100vh;
  transform: translate(-50%, -50%);
  content: " ";
  overflow: hidden;
  
    // Fallback for old browsers
  background: #16222A;

  background: -webkit-linear-gradient(to left, #16222A , #3A6073);
  background: linear-gradient(to left, #16222A , #3A6073);
  
  background-size: cover;
  
  background-image: url(https://cldup.com/gn3s3Fg75t.gif);
  
   color: $color-text;
}

The JavaScript code section:

(function() {
  var SELECTOR_SCREEN_ELEMENT = '.screen';
  var SELECTOR_SWITCHER_TV = '#switcher-tv';
  
  var isTurnedOn = true;
  
  var timeline;
  
  function buildTimeline() {
    timeline = new TimelineMax({
      paused: true
    });
    
    timeline
    .to(SELECTOR_SCREEN_ELEMENT, .2, {
      width: '100vw',
      height: '2px',
      background: '#ffffff',
      ease: Power2.easeOut
    })
    .to(SELECTOR_SCREEN_ELEMENT, .2, {
      width: '0',
      height: '0',
      background: '#ffffff'
    });
  }
  
  function toggleSwitcherTV() {
    if (isTurnedOn) {
      timeline.restart();
    }
    
    if (!isTurnedOn) {
      timeline.reverse();
    }
    
    isTurnedOn = !isTurnedOn;
  }
  
  // Initialize
  $(document).ready(buildTimeline);
  
  // Bindings
  $(document).on('click', SELECTOR_SWITCHER_TV, function() {
    toggleSwitcherTV();
  });
})();

Finally, the compiled CSS code:

body {
  font-family: sans-serif;
  background-color: #111111;
}
a {
  color: #ff5f2e;
}
a:hover {
  color: #fcbe32;
}
button {
  position: fixed;
  right: 20px;
  bottom: 20px;
  padding: 20px;
  font-weight: 700;
  font-size: 16px;
}
h1 {
  text-align: center;
}
.screen {
  background-color: #e1eef6;
  position: fixed;
  top: 50%;
  left: 50%;
  width: 100vw;
  height: 100vh;
  transform: translate(-50%, -50%);
  content: " ";
  overflow: hidden;
  background: #16222A;
  background: -webkit-linear-gradient(to left, #16222A, #3A6073);
  background: linear-gradient(to left, #16222A, #3A6073);
  background-size: cover;
  background-image: url(https://cldup.com/gn3s3Fg75t.gif);
  color: #e1eef6;
}

Answer â„–1

Opt for a straightforward approach by adding a class when the user clicks to turn on the TV and doing the same in reverse when turning it off. Apply CSS styling to elements with this new class.

$("#switcher-tv").click(function(){
  $(".screen").toggleClass("tvON");
});
.screen.tvON{
  background-image: url(https://cldup.com/gn3s3Fg75t.gif);
}

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

Is it possible to include a submenu within a md-tooltip?

I have set up an md-tooltip for a sidebar, but I would like to enhance it by enabling a submenu option. For instance, if there is a Contacts submenu under the Profile menu, I want to be able to access Contacts when hovering over the Menu Icon. The tooltip ...

What strategies can be implemented to increase the number of backlinks?

<script type="text/javascript"> $(document).ready(function() { var linkas = $("#button").attr("value"); $('#button').click(function(){ $.get(linkas, function(data){ $(' ...

What is the reason behind genNewColor function's malfunction?

I'm having trouble with this code that is supposed to generate a random color. The CSS formatting works perfectly, but I suspect there might be an issue with the function itself. When I try to run the code, it doesn't produce any error messages â ...

React is currently in the process of downloading images that were not fetched during

(Update: Initially, I suspected React Router was the cause of this issue. However, after eliminating React Router from the codebase, the problem persists. Therefore, I have extensively revised this inquiry.) Situation: I am dealing with paginated pages th ...

Using jQuery AJAX to dynamically update two sections of a webpage by executing scripts embedded in the server response

I'm currently utilizing jQuery AJAX to load and update a specific section of my webpage. Below is the jQuery function in my main page: function updateCategories(){ catList = $('#cat_list'); catList.hide(); //sending the post re ...

"Troubleshooting an issue with ng-model not functioning properly with radio buttons in Angular

I'm a newcomer to Angular and I'm attempting to retrieve the value of the radio button selected by the user using ng-model. However, I'm not seeing any output in "selected contact". Check out My HTML below: <!doctype html> <html n ...

Is there a way to programmatically display an edit text field on an HTML page?

Currently, I have a straightforward task which involves pulling data from a MySQL database to display in an HTML table. Here is the current script: <html> <body> <table border='1'> <?php mysql_connect('url',&ap ...

Creating movement in an SVG patterned background

Currently in the process of designing a brand new website using NextJS and Tailwind. I am looking to incorporate an infinitely translating background effect that moves towards the bottom right, similar to this example (but with my own unique pattern): htt ...

Is the HTML encoded character displaying strangely due to spacing issues?

I'm having trouble creating a link with a right chevron that has a larger font size. The issue I'm facing is that the right chevron appears to have excessive top margin, causing a significant gap between it and the line above. Additionally, I wa ...

Error message encountered: "TypeError with jQuery autocomplete and ajax

I can't seem to figure out why I keep encountering this error when trying to set up autocomplete with ajax source. "Uncaught TypeError: Cannot read property 'length' of undefined" Below is the route I have in express. exports.findAllIrds ...

Obtain scope in AngularJS using object ID

Is it possible to retrieve the specific scope of an object by accessing it through an ID? I am currently using angular ui tree for a navigation menu. However, I face an issue where after adding a subitem and saving the navigation into a mysql database, th ...

Generating small image previews in JavaScript without distorting proportions

I am currently working on a client-side Drag and Drop file upload script as a bookmarklet. To prepare for the upload process, I am utilizing the File API to convert the images into base64 format and showcase them as thumbnails. These are examples of how m ...

What is the best way to align a button with a title on the right side of a page?

Is it possible to align my button to the top right, at the same height as the title in my design? Here is an example: view image here This is what I have achieved so far: view image here I suspect there may be an issue with my divs. The button doesn&a ...

The property linerGradiant of r cannot be destructured because it is not defined

I encountered the error "Cannot destructure property linerGradiant of r as it is undefined" during runtime, making it difficult to debug. The issue seems to stem from the compiled JS file, which is hard to read. The function is defined as follows: functio ...

What are the steps to utilizing dynamic data from a file in JavaScript?

I've got a Python script constantly generating data, and I'm looking to use that data to make some changes to an object in JavaScript. Is there a method to accomplish this? ...

Changing my PHP contact form from method GET to POST is causing it to malfunction

Feel free to check out my contact form on benlevywebdesign.com, located at the bottom of the page. <form id="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="text/plain" method="get"> <fieldset> ...

When applying the `display:block` and `width:100%` attributes to an HTML table cell, it may

My issue lies in the usage of DISPLAY:BLOCK for the td cells with a class of column. Despite applying it appropriately, the logo and CALL US td cells do not stack vertically when resized to mobile size. While they are supposed to take up 100% width, they ...

What is the proper usage of a jwt token?

I'm completely new to this and I've dedicated all my time to figuring out how to create a mechanism for generating JWT tokens. These tokens are necessary for identifying the 'signed in' status of users. I opted for FastAPI, and after s ...

Experiencing a hiccup in your jQuery animation?

Click here to access the fiddle demonstrating the issue. A situation arises where a span with display: inline-block houses another span that is being slowly hidden. The container span unexpectedly shifts back to its original position once the hiding proces ...

Swiper JS eternal carousel transition

While trying to create an endless carousel using swiper js on my website, I encountered a slight vibration between the slides that makes it seem like the images are flickering. Despite my efforts, I have been unable to find a solution to this issue. Any as ...