Interactive hexagon shape with dynamic image content on click using a combination of CSS, HTML,

As a beginner in CSS, HTML, and JavaScript, I came across a code snippet to create a pattern of hexagons with images (refer to the first code below). My goal is to change the image displayed on a clicked hexagon to another picture (see second code).

First Code: The following includes JavaScript, CSS, and HTML.

//My JavaScript logic remains to be determined
/**
 * Generate repeating hexagonal pattern with CSS3 (SO) - 1 element/ hexagon !!!
 * http://stackoverflow.com/q/10062887/1397351
 */

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
.row {
  margin: -18% 0;
  text-align: center;
}
/* remaining CSS styling omitted for brevity */
<!-- content to be placed inside <body>...</body> -->

<div class='row'>
  <div class='hexagon'></div>
</div>

<!-- additional row elements omitted for brevity -->

Second Code: Below contains JavaScript, CSS, and HTML.

var bodyObj, className, index;

bodyObj = document.getElementById('body');
index = 1;
className = [
  'imageOne',
  'imageTwo'
];

function updateIndex() {
  if (index === 0) {
    index = 1;
  } else {
    index = 0;
  }
}

// JavaScript event listener not fully shown for brevity
html,
body,
#body {
  height: 100%;
  width: 100%;
}
/* remaining CSS styles for classes imageOne and imageTwo are omitted */
<div id="body" class="imageOne"></div>

Answer №1

Outcome: jsfiddle

Introducing a new class .hexagon-hide (similar to .hexagon but with distinct background images), the JQuery function is incorporated to alternate between .hexagon and .hexagon-hide upon clicking in the hexagon div.

$( ".hexagon" ).click(function() {
  $( this ).toggleClass('hexagon hexagon-hide');
});
/**
 * Creating a repeating hexagonal pattern using CSS3 (SO) - 1 element/ hexagon !!!
 * http://stackoverflow.com/q/10062887/1397351
 */

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
.row {
  margin: -18% 0;
  text-align: center;
}
.row:first-child {
  margin-top: -20%;
}
.hexagon, .hexagon-hide {
  position: relative;
  display: inline-block;
  overflow: hidden;
  margin: 0 8.5%;
  padding: 16%;
  transform: rotate(30deg) skewY(30deg) scaleX(.866);
  /* .866 = sqrt(3)/2 */
}
.hexagon:before, .hexagon-hide:before,
.content:after {
  display: block;
  position: absolute;
  /* 86.6% = (sqrt(3)/2)*100% = .866*100% */
  top: 6.7%;
  right: 0;
  bottom: 6.7%;
  left: 0;
  /* 6.7% = (100% -86.6%)/2 */
  transform: scaleX(1.155) skewY(-30deg) rotate(-30deg);
  /* 1.155 = 2/sqrt(3) */
  background-color: rgba(30, 144, 255, .56);
  background-size: cover;
  content: '';
}
t:after {
  content: attr(data-content);
}
.row:first-child .hexagon:first-child:before {
  ...
}

...


.row:nth-child(5) .hexagon-first-child:before {
  ...

}



.row:first-child .hexagon-hide-first-child:before {
  ...
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<!-- Content goes inside <body>…</body> -->

<div class='row'>
  ...
</div>

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

Methods for smoothly animating an image to move up and down using CSS

I'm new to CSS and I was wondering if it's possible to create a smooth vertical movement effect for an image using CSS. Something like shown in this link: .... ...

Instructions for placing the navigation links below the navigation brand

Could you please advise me on how to center the navigation links below the brand logo? I am new to coding and just starting out. Here is a link to a page that demonstrates what I am trying to achieve: In this example, the brand name is "a fork and a pe ...

Discovering active path while utilizing dynamic routes - here's how!

Whenever I click on the project element in my HeadlessCMS with the URL /projects/slug-name, the projects button in my Navbar (LinkItem) does not highlight with a background color (bgColor). How can I modify this line of code: const active = path === href / ...

Utilize jQuery to choose a specific tab

I have implemented a jquery tab in my UI. I want to have the second tab selected when the page loads, instead of defaulting to the tab with the "Active" class. Here is my HTML tab code: <div class="typo"> <div class="container-fluid"> ...

How do I access Google Chrome and perform a Google image search within a Node.js/Electron program?

I am currently working on incorporating Google Image search into my Electron-based photo application. My goal is to have users click on a button labeled "Search Google for Image," which will then open up Chrome (if installed) with the local image file in ...

How to update the state of a component in the root layout from its child components in Next JS 13 with the app router?

I am in the process of upgrading a Next JS app project to utilize the App Router. Within the layout.js (the root layout), there is a Logo component that will be visible on all routes. Within the RootLayout, I am managing a state variable to modify the ap ...

What is the best way to cluster related data points and display them together?

In order to efficiently manage the data I receive, it is necessary to group it based on specific criteria and display these groups in the user interface. "ServiceRequest": [ {"Status": "Re-Open", },{ "Status": "Open", ...

Discovering the total of varying inputs in ReactJS

//this is the API response: { "message": "success", "code": 100, "data": { "inCourseCategories": [ { "_id": "62b842f09184bf2330e6f506", "course": "601a67e6db65fb15946e6b6f ...

`how to extract the href attribute value from the hyperlink that triggered a modal in jQuery UI`

Recently, I began diving into Jquery and javascript coding. As a beginner, it feels odd to refer to myself as a noob due to my age. Here's the scenario: I have a hyperlink that triggers a dialogue box and sets a cookie. The dialog asks the user, "Are ...

Issue with retrieving relative time using Moment.js - fromNow()

I want to utilize moment.js to display relative time fromNow(), but I am encountering an issue where the values are being rounded and showing higher durations instead of exact completion times. For example, moment().subtract('s',110).fromNow() / ...

Spilling over into the adjacent DIV

<html> <head> <title>Pixafy</title> <style> html { background: url(wp.jpg) no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; backgrou ...

Experiencing an inexplicable blurring effect on the modal window

Introduction - I've implemented a feature where multiple modal windows can be opened on top of each other and closed sequentially. Recently, I added a blur effect that makes the background go blurry when a modal window is open. Subsequently opening an ...

Executing an Ajax request to a document containing <script> elements

Recently, I developed a sample page that effectively mimics table layout pages but without relying on the traditional mobile-unfriendly table features. The structure of the page is as follows: <html> ... <body> <div type="page" ...

Using multiple where clauses in Firebase for advanced querying

Let me explain the scenario. Within my database, there is a Users collection. Some users have a property called userType, while others do not. The values for userType can either be person or company. I want to retrieve only the users that have the userTyp ...

I'm curious if it's possible to perform background tasks using React Native Expo with the example I have in mind

Is there a way to perform background tasks in React Native Expo? I am looking to make a POST request every 5 seconds and log the results. Can someone guide me on how to achieve this using the example from this site? I would like to modify the given exampl ...

Is localStorage.getItem() method in NextJS components behaving differently?

I'm working on my nextjs application and I wanted to utilize the power of localstorage for storing important data throughout my app. Within the pages directory, specifically in the [slug].tsx file, I implemented the following logic: export default fu ...

Executing MySQL queries synchronously in Node.js

When working with NodeJS and mysql2 to save data in a database, there are times when I need to perform database saves synchronously. An example of this is below: if(rent.client.id === 0){ //Save client connection.query('INSERT INTO clients (n ...

Navigating with Link in React Router DOM v5 to successfully pass and catch data

In the process of developing a basic chat application, I encountered an issue with passing the username via the Link component. Below is the relevant code snippet: <Link to={{ pathname: `/${room}`, state: { the: user } }}> Enter room </Link> ...

What potential issues can arise when importing a default export alongside a named export and using webpack in conjunction with babel?

I have two distinct constructors in my codebase: SignUp and GoogleSignIn. They are structured as follows: import SignUp, {gapi_promise} from "./SignUp"; /** * * @param element * @extends SignUp * @constructor */ function GoogleSignIn(element){ SignUp ...

Using PHP to create multiple div elements and then concealing them all

I'm having an issue with the hide feature not working. My intention is to hide each dynamically generated div and gain control over them, but the problem seems to lie in the div id incrementing. Any assistance would be greatly appreciated! <?php $ ...