Is it possible to apply styles to javascript elements without relying on img class? Additionally, how can I incorporate an onclick button while maintaining a fully functional navigation bar?

My current project involves creating an interactive collage where users can click around and have pictures pop up at the clicked location. The functionality works as intended, but now I'm facing issues with the navigation bar not being clickable. Additionally, using images in CSS for this collage is causing some unintended effects on other parts of my website.

const images = [
  "collage1.webp", "collage2.jpg", "collage3.jpg", "collage4.jpg", "collage5.jpg", "collage6.jpg"
]

let i = 0


function placeImage(x, y) {

  const nextImage = images[i]


  const img = document.createElement("img")
  img.setAttribute("src", nextImage)
  img.style.left = x + "px"
  img.style.top = y + "px"
  img.style.transform = "translate(-50%, -50%) scale(0.5) rotate(" + (Math.random() * 20 - 10) + "deg)"
  document.body.appendChild(img)


  i = i + 1

  if (i >= images.length) {
    i = 0
  }
}

document.addEventListener("click", function(event) {
  event.preventDefault()
  placeImage(event.pageX, event.pageY)
})

document.addEventListener("touchend", function(event) {
  event.preventDefault()
  placeImage(event.pageX, event.pageY)
})
body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
  color: white;
  background-image: url("background3.jpg");
}

.topnav {
  overflow: hidden;
  /*background-color: #214a63;*/
  /*#0A1128*/
  /*display:flex;*/
  justify-content: left;
  align-items: left;
}

.topnav a {
  float: left;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover {
  background-color: #ddd;
  color: black;
}

.topnav a.active {
  background-color: #4db1f0;
  color: white;
}


/*test for favicon menu item*/

.topnav a.split {
  float: right;
  padding: 14px 16px;
}

#footer {
  position: fixed;
  bottom: 0;
  left: 0;
}

#wrap {
  min-height: 100%;
}

#wrapper {
  width: 600px;
  margin: 80px auto;
  text-align: center;
}

h1 {
  text-align: center;
}


/*
img {
  width: 400px;
  height: auto;
}*/

.center {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 40%;
}


/*for floating selfie*/

.flier.self {
  width: 400px;
  height: auto;
}


/*for linked in logo*/

.linkedin {
  height: 40px;
  width: auto;
}


/* for the about me page (looks good but gets rid of linked in image and cant click tabs)*/

@keyframes fadein {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 100;
  }
}

img {
  position: absolute;
  top: 500px;
  left: 400px;
  transform: translate(-50%, -50%) scale(0.5);
  animation: fadein 0.5;
}
<body>
  <div class="topnav">
    <a href="index.html">Home</a>
    <a class="active" href="#aboutMe">About Me</a>
    <a href="vacation.html">Vacation Spots</a>
    <a href="eightBall.html">Magic Eight Ball</a>
    <a href="dog.html">My Dog</a>
    <a href="linked in goes here" target="_blank" class="split"> <img src="LI-In-Bug.png" class="linkedin"></a>
  </div>

  <h1>About Me</h1>

  <script src="collage.js"></script>

</body>

Answer №1

const images = [
  "collage1.webp", "collage2.jpg", "collage3.jpg", "collage4.jpg", "collage5.jpg", "collage6.jpg"
]

let i = 0


function createImage(x, y) {

  const nextImg = images[i]


  const imgTag = document.createElement("img")
  imgTag.setAttribute("src", nextImg)
  imgTag.style.left = x + "px"
  imgTag.style.top = y + "px"
  imgTag.style.transform = "translate(-50%, -50%) scale(0.5) rotate(" + (Math.random() * 20 - 10) + "deg)"
  imgTag.classList.add("collage")
  document.body.appendChild(imgTag)


  i = i + 1

  if (i >= images.length) {
    i = 0
  }
}

document.addEventListener("click", function(event) {
 
  createImage(event.pageX, event.pageY)
})

document.addEventListener("touchend", function(event) {
  
  createImage(event.pageX, event.pageY)
})
body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
  color: white;
  background-image: url("background3.jpg");
}

.topnav {
  overflow: hidden;
  /*background-color: #214a63;*/
  /*#0A1128*/
  /*display:flex;*/
  justify-content: left;
  align-items: left;
}

.topnav a {
  float: left;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover {
  background-color: #ddd;
  color: black;
}

.topnav a.active {
  background-color: #4db1f0;
  color: white;
}


/*test for favicon menu item*/

.topnav a.split {
  float: right;
  padding: 14px 16px;
}

#footer {
  position: fixed;
  bottom: 0;
  left: 0;
}

#wrap {
  min-height: 100%;
}

#wrapper {
  width: 600px;
  margin: 80px auto;
  text-align: center;
}

h1 {
  text-align: center;
}


/*
img {
  width: 400px;
  height: auto;
}*/

.center {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 40%;
}


/*for floating selfie*/

.flier.self {
  width: 400px;
  height: auto;
}


/*for linked in logo*/

.linkedin {
  height: 40px;
  width: auto;
}


/* for the about me page (looks good but gets rid of linked in image and cant click tabs)*/

@keyframes fadein {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 100;
  }
}

.collage {
  position: absolute;
 
}
<body>
  <div class="topnav">
    <a href="index.html">Home</a>
    <a class="active" href="#aboutMe">About Me</a>
    <a href="vacation.html">Vacation Spots</a>
    <a href="eightBall.html">Magic Eight Ball</a>
    <a href="dog.html">My Dog</a>
    <a href="linked in goes here" target="_blank" class="split"> <img src="LI-In-Bug.png" class="linkedin"></a>
  </div>

  <h1>About Me</h1>

  <script src="collagescript.js"></script>

</body>

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

Keep rolling the dice until you hit the target number

Currently, I am in the process of learning JavaScript and one of my projects involves creating a webpage that features two dice images, an input box, and a button. The objective is for users to input a value, click the button, and then see how many rolls i ...

"I encountered an error while sorting lists in Vue 3 - the function this.lists.sort is not

Creating a Vue 3 front-end template: <template> <div class="container"> <router-link to="/user/create" class="btn btn-success mt-5 mb-5">Add New</router-link> <table class=" ...

Horizontal scrollbar appearing on larger screens

My website utilizes bootstrap and some custom css. I have a specific css class named "full-width" designed to break out of the parent bootstrap "container" in order to make the background color extend across the entire width of the screen. However, I recen ...

Require assistance in generating three replicas of an object rather than references as it currently operates

I am encountering an issue with my code where I seem to be creating 4 references to the same object instead of 4 unique objects. When I modify a value in groupDataArrays, the same value gets updated in groupDataArraysOfficial, groupDataArraysValid, and gro ...

AngularJS $log - display line numbers

When using angularjs $log in chrome, it displays the line as: angular.js:9037. I'm looking to have the line number where I actually call this method shown instead. (Displaying my js name and the correct line). Is there a way to achieve this since Angu ...

How can I convert a UTC time field from MySQL into JavaScript?

I'm struggling to pinpoint the error in my date/time difference calculations. It seems to be related to timezone variations, but I can't figure out exactly where the problem lies. Below are all the components involved - can you help me identify t ...

Customizing ExtJS Themes for Ext.panel.Panel

In my current project using ExtJS 4.1.1a, I am working on creating a new theme for a "tabbedPane" and a normal Panel with an "Accordion" layout. However, I am facing difficulty in changing the color of the headers for both elements. I wish to customize the ...

An error occurred as the Serverless Function timed out while attempting to load a dynamic route in Next.js version 13

I have a basic Next.js application with the following route structure: /contentA/ - Static - Initial load: 103 kB /contentA/[paramA]/groups - SSG - Initial load: 120 kB /contentB/[paramA]/[paramB]/[paramC] - SSR (client component) - Initial load: 103 kB ...

Javascript: Troubleshooting Unexpected Variable Behavior in HTML Code

My issue is as follows: I am attempting to retrieve text from a JSON file in my HTML. In the header of my HTML, I have linked a tag with the following code: var language = ""; var langDoc = null; //Function to change the value function setLang() { v ...

Attempting to engage with the like button on a Facebook page through Python's Selenium seems to be problematic

Here is the code for adding a (Facebook page like button): WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div/div[4]/div/div[1]/div/div[1]/div[2]/div[2]/div/div/div[3]/div/div[1]'))).click() The clic ...

Load Angular scripts only when needed

I need to develop an application that can dynamically register new Angular Controllers obtained from a script. This application should load the minimum necessary scripts at startup and then fetch additional scripts as needed from other modules. Here' ...

Client-Specific User Portal

Looking for some support on the back end of things. We are in the process of creating a portal where users will be directed to their specific landing pages upon logging in, providing access to files they have signed up for. Our team is exploring the use ...

Leverage JSON data using props in React JS

Currently, my data is formatted in JSON and successfully populated into props known as foxFooterData. Upon inspecting the console.log result of foxFooterData.data, the following information is visible: https://i.sstatic.net/8htNG.png I am attempting to r ...

Downloading a zip file using PHP works successfully when initiated directly, but encounters errors when attempted through a web application

I have been working on a PHP script that generates a zip file and allows it to be downloaded from the browser. The download function in the code snippet below: download.php // ensure client receives download if (headers_sent()) { echo 'HTTP head ...

The resizing of images is limited to when the browser is refreshed for responsive

Hey there, I'm currently facing an issue with a responsive image gallery using jQuery cycle. If you want to check out my fiddle, here's the link - https://jsfiddle.net/qxyodctm/5/ <html>See jsfiddle</html> The problem occurs when t ...

What reasons underlie the existence of various methods for importing Modules in JavaScript?

I'm confused about the distinctions when it comes to importing a JavaScript module in various ways such as: CommonJS ES5 ES6 NodeJS Typescript What is the reason for having multiple methods of importing JavaScript modules? Is the concept of a "modu ...

Error message "e.nodename undefined when set to setTimeout" was encountered

I developed a unique piece of code for input boxes located at the top of different tables. By using a class='filt', the table can be filtered based on the inputs provided. However, most of the inputs consist of more than one letter, so I wanted t ...

How can I detect when an image is loaded in a specific container division using jQuery?

I have a container with multiple images inside, and I would like to capture an event each time an image finishes loading. For example: <div id="container"> <img src="..." /> <img src="..." /> <img src="..." /> ...

Does the react-google-login library utilize the services provided by Google Identity?

Currently incorporating the react-google-login library (https://www.npmjs.com/package/react-google-login/v/5.2.2) in my JavaScript codebase to grant users access to my website. Can anyone confirm whether this library utilizes "Google Identity Services" or ...

Tracking the latency of WebSockets communications

We are in the process of developing a web application that is highly responsive to latency and utilizes websockets (or a Flash fallback) for message transmission to the server. While there is a fantastic tool known as Yahoo Boomerang for measuring bandwidt ...