The code that runs perfectly in one IDE may not function properly in another - specifically for html and css

Here is the code snippet that I have been working with:

document.addEventListener("DOMContentLoaded", function(event) { 

// Get a reference to the <path>
var path = document.querySelector('#star-path');

// Get length of path... ~577px in this case
var pathLength = path.getTotalLength();

// Make very long dashes (the length of the path itself)
path.style.strokeDasharray = pathLength + ' ' + pathLength;

// Offset the dashes so the it appears hidden entirely
path.style.strokeDashoffset = pathLength;

// Jake Archibald says so
// https://jakearchibald.com/2013/animated-line-drawing-svg/
path.getBoundingClientRect();

// When the page scrolls...
window.addEventListener("scroll", function(e) {
 
  // What % down is it? 
  // https://stackoverflow.com/questions/2387136/cross-browser-method-to-determine-vertical-scroll-percentage-in-javascript/2387222#2387222
  // Had to try three or four differnet methods here. Kind of a cross-browser nightmare.
  var scrollPercentage = (document.documentElement.scrollTop + document.body.scrollTop) / (document.documentElement.scrollHeight - document.documentElement.clientHeight);
    
  // Length to offset the dashes
  var drawLength = pathLength * scrollPercentage;
  
  // Draw in reverse
  path.style.strokeDashoffset = pathLength - drawLength;
    
  // When complete, remove the dash array, otherwise shape isn't quite sharp
 // Accounts for fuzzy math
  if (scrollPercentage >= 0.99) {
    path.style.strokeDasharray = "none";
    path.style.fill = "#47AF9A";
    
  } else {
  path.style.fill = "none";
    path.style.strokeDasharray = pathLength + ' ' + pathLength;
  }
  
});
});
body {
  /* feel free to change height */
  height: 5000px;
  background: linear-gradient(
    to bottom,
    orange,
    darkblue
  );
}

h1 {
  font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif; 
  font-weight: 300;
  color: white;
  text-transform: uppercase;
  text-align: center;
  font-size: 22px;
  letter-spacing: 5px;
  font-weight: 100;
  padding: 25px 15px;
  text-shadow: 1px 1px 1px #333;
}

#star-svg {
  position: fixed;
  top: 50%;
  left: 50%;
  width: 150px;
  height: 150px;
  margin: -75px 0 0 -75px;
}



Resources
<header>
  
    <link href="style.css" rel="stylesheet"/>
  <script src="script.js"></script>
</header>
  
  <body>
  <h1>Scroll-to-draw</h1>

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200.6" id="star-svg">
  <path fill="none" stroke="white" stroke-width="2" id="star-path" d="M45.33 78.22L87.67 78.22L87.67 133L121.05 133L121.05 0L87.67 0L87.67 49.33L45.33 49.33L45.33 0L11.95 0L11.95 133L45.33 133L45.33 78.22Z"></svg>
  </body>

The above code works fine here, but unfortunately, when I tried running it on the IDE I am using, the animation did not render as expected. Instead of drawing lines upon scrolling, only the overall output of the letter H was displayed. If you could spare a moment to review the code on this platform: https://repl.it/join/xfuwhnru-hussainomer, it would be greatly appreciated.

P.S. It is possible that there might be a missing reference in my code when used in that particular IDE. The functionality seems to work here due to Stack Overflow's unique setup where it creates a reference automatically, which may not be replicated in every other IDE. Any insights or suggestions on how to tackle this issue?

Answer №1

Your HTML code is not properly structured:

Take a look at the source code here:

It should only contain the following:

<header>
  
    <link href="style.css" rel="stylesheet"/>
  <script src="script.js"></script>
</header>
  
  <body>
  <h1>Scroll-to-draw</h1>

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200.6" id="star-svg">
  <path fill="none" stroke="white" stroke-width="2" id="star-path" d="M45.33 78.22L87.67 78.22L87.67 133L121.05 133L121.05 0L87.67 0L87.67 49.33L45.33 49.33L45.33 0L11.95 0L11.95 133L45.33 133L45.33 78.22Z"></svg>
  </body>
  • Make sure to include a DOCTYPE declaration at the beginning
  • Add an opening <html> tag to enclose your content
  • The <header> element should be within the <body>, consider using <head> instead

Once you fix these issues, your code will display correctly:

<!DOCTYPE html>
<html>
<head>
<link href="style.css" rel="stylesheet"/>
... etc
</head>
<body>
... etc

It's crucial to specify a doctype to ensure proper rendering. Without it, browsers may enter Quirks mode and interpret your code incorrectly, leading to unexpected results.

A missing DOCTYPE can cause inconsistencies in how different browsers render your content. It is essential for defining the version of HTML being used and ensuring proper display.

In this case, omitting the DOCTYPE affects the calculation of

document.documentElement.clientHeight
, resulting in incorrect height measurements and layout issues.

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

The main menu vanishes when you hover over it after the affix class is activated

I am currently working on creating a horizontal dropdown menu that expands on mouseover and collapses on mouseout under one of the main menu items. The functionality of this effect is working fine, except for one issue. When I scroll down and activate the ...

Creating a navigation bar in React using react-scroll

Is anyone able to assist me with resolving the issue I am facing similar to what was discussed in React bootstrap navbar collapse not working? The problem is that although everything seems to be functioning properly, the navbar does not collapse when cli ...

Tips for enlarging a specific parent element using Jquery

How can I modify my jquery menu to expand only the child under a particular parent when clicked, hiding all other children under different parents? I have obtained the parent id and need help in passing it to the code. This is my current code: <scri ...

Innovative and interactive animated data display

I've taken inspiration from an example and expanded the code to achieve the following: Dynamic height adjustment Accessibility without JavaScript enabled Is this implementation correct? Can it be expected to function properly on most browsers? You ...

Can CKEditor be integrated with Mutation Observer? If so, how can this be achieved?

Trying to detect changes in the current CKEditor content. The goal is to identify which element's content has been modified when a user writes multiple paragraphs. Not well-versed in JavaScript or jQuery, but managed to come up with this code after s ...

Mastering the use of useMasterKey in Parse with Node.js: A guide

Greetings everyone, I have developed my app using parse.com but now I need to utilize the master key as some users have administrative roles. Initially, I attempted to use the JS SDK but was advised to switch to Cloud Code. So, I tried using Express and ...

trigger a CSS animation using JavaScript upon a specified event

I am attempting to incorporate a class for an autogrow animation effect to a textarea element. Here is a demo: http://jsfiddle.net/d0kmg7d3/15/ var tx = document.getElementsByTagName('textarea'); for (var i = 0; i < tx.length; i++) { tx[i] ...

Outputting HTML using JavaScript following an AJAX request

Let's consider a scenario where I have 3 PHP pages: Page1 is the main page that the user is currently viewing. Page2 is embedded within Page1. It contains a list of items with a delete button next to each item. Page3 is a parsing file where I send i ...

Fleeing from the clutches of the $.ajax({ success: function() }) labyrinth

There is a common scenario where AJAX responses dictate the flow of actions, often leading to nested AJAX responses. However, this results in a clutter of presentation-specific code within the success() callback: $.ajax({ ... success: function ...

In Vue.js, it is not possible to modify a component variable within a callback function

I have implemented a custom Login.vue component using vue.js to allow users to log in to my application through AWS Cognito. The authentication process is handled by the ___.authenticateUser() method, which initiates a session with Cognito. Below is the co ...

Tips for eliminating the border from a Bootstrap dropdown menu in the navigation bar

I am utilizing the Bootstrap 5 navigation bar and trying to figure out how to remove the blue border from the "Dropdown" when clicked on. Despite my efforts to find a solution, I have been unsuccessful. <nav class="navbar navbar-expand-lg navbar ...

Tips for retrieving an element's outerHTML, innerHTML, and text content using JavaScript

I am new to the protractor framework and I have been struggling to find a way to access, using outerHTML/InnerHTML/getText(), the child elements in order to test if an <img> element is being displayed on a view. Specifically, I am working with an ng- ...

Change from using fs.writeFileSync to fs.writeFile

I have a question about changing fs.writeFileSync to fs.writeFile const users = { "(user id)": { "balance": 28, "lastClaim": 1612012406047, "lastWork": 1612013463181, "workersCount": 1, ...

Guide to creating a new browser tab in Java Script with customizable parameters

Here is a function I created to open a new window with a specific URL and pass certain parameters. function viewWeightAge() { var userNIC = document.getElementById("NICNo"); var childName = document.getElementById("c ...

Executing npm scripts in Node.js

Trying to move away from using the likes of Grunt or Gulp in my projects, I've been exploring npm-scripts as a potential replacement. While npm-scripts makes use of `package.json`, I've found that more advanced build processes require command lin ...

Ways to extract the first name and email address from a JSON payload

{ "userID": 1, "userHandle": "username", "first_name": "firstname", "last_name": "lname", "middle_initial": null, "email_address": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e203d250e29232f27 ...

Utilizing Ajax to auto-fill form fields with data retrieved from a PHP response object

To begin, my method involves utilizing the $.getJSON() function to send the user ID through a GET request. Here is the JavaScript code snippet: $.getJSON("this_file.php?id=1", null, function(data){ $('input#id').val(data.id) ...

Comparison between Javascript and C loops for handling complex arrays

I'm currently dealing with a challenge in my node.js project. The issue I'm facing is that a complex loop in my code is taking around 200-300 ms to complete, which seems quite high. I'm considering converting this particular piece of code to ...

integrating jquery with Ruby on Rails version 3

I am facing an issue in my rails application where I am trying to implement a jQuery effect. Specifically, I want a button that will hide the content within a paragraph tag when clicked. Despite trying various approaches, I have been unable to get it worki ...

Tips for perfectly centering a SPAN element within a DIV both vertically and horizontally

Here is an example of my HTML code: <div id="slideClick"> <div style="padding: 0; margin: 0 auto; width: 100%; height: 100%; text-align: center; border: 1px solid blue;"> <span class="sideMsg">MESSAGES</span> </d ...