Skip to content

Instantly share code, notes, and snippets.

@shopglobal
Created September 24, 2024 20:23
Show Gist options
  • Save shopglobal/abea90c5c800ddc7bd2ab9a21e0b6e31 to your computer and use it in GitHub Desktop.
Save shopglobal/abea90c5c800ddc7bd2ab9a21e0b6e31 to your computer and use it in GitHub Desktop.
Website with 3d elements

Creating a cool website with 3D elements and a hero slider involves using modern front-end technologies like HTML, CSS, JavaScript (and libraries like Three.js for 3D), and CSS frameworks like Bootstrap for layout and sliders. Here’s a step-by-step approach to building the site:

1. Basic Structure (HTML)

Start with a simple HTML structure:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>3D Elements Website</title>
  <link rel="stylesheet" href="styles.css">
  <!-- Include necessary JavaScript libraries -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
</head>
<body>

  <!-- Hero Section with Slider -->
  <section class="hero">
    <div class="slider">
      <div class="slide active">
        <h1>Welcome to the Future</h1>
        <p>Explore the new era of technology</p>
      </div>
      <div class="slide">
        <h1>3D Interactive Designs</h1>
        <p>Experience visually stunning content</p>
      </div>
      <div class="slide">
        <h1>Fast and Responsive</h1>
        <p>Optimized for all devices</p>
      </div>
    </div>
    <div class="slider-controls">
      <span class="prev"></span>
      <span class="next"></span>
    </div>
  </section>

  <!-- 3D Canvas -->
  <div id="threeDCanvas"></div>

  <!-- JavaScript to handle the 3D elements and slider -->
  <script src="scripts.js"></script>
</body>
</html>

2. Styling (CSS)

You can style the 3D canvas and the hero slider in a sleek and modern way:

/* General resets */
body, html {
  margin: 0;
  padding: 0;
  overflow-x: hidden;
  font-family: 'Arial', sans-serif;
}

.hero {
  position: relative;
  height: 100vh;
  width: 100vw;
  background: #111;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  text-align: center;
}

.slider {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.slide {
  opacity: 0;
  transition: opacity 1s ease;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.slide.active {
  opacity: 1;
}

.slider-controls {
  position: absolute;
  bottom: 10%;
  width: 100%;
  display: flex;
  justify-content: space-between;
  padding: 0 2rem;
}

.slider-controls span {
  cursor: pointer;
  font-size: 2rem;
  color: #fff;
}

#threeDCanvas {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: -1;
}

3. 3D Elements and Slider (JavaScript)

Next, use JavaScript to manage the slider and add 3D elements:

// Slider functionality
let currentSlide = 0;
const slides = document.querySelectorAll('.slide');
const totalSlides = slides.length;

document.querySelector('.next').addEventListener('click', () => {
  slides[currentSlide].classList.remove('active');
  currentSlide = (currentSlide + 1) % totalSlides;
  slides[currentSlide].classList.add('active');
});

document.querySelector('.prev').addEventListener('click', () => {
  slides[currentSlide].classList.remove('active');
  currentSlide = (currentSlide - 1 + totalSlides) % totalSlides;
  slides[currentSlide].classList.add('active');
});

// Three.js 3D elements setup
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.getElementById('threeDCanvas').appendChild(renderer.domElement);

// Basic 3D object (spinning cube)
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
camera.position.z = 5;

function animate() {
  requestAnimationFrame(animate);
  cube.rotation.x += 0.01;
  cube.rotation.y += 0.01;
  renderer.render(scene, camera);
}
animate();

4. Enhancements

  • 3D Elements: You can replace the spinning cube with more complex 3D objects, like 3D models or particles, by loading .obj or .glb files using the GLTFLoader in Three.js.

  • Hero Slider: Add auto-play for the slider, smooth transitions, and more interactive elements, such as clickable buttons for each slide.

This website will have a cool, interactive 3D background powered by Three.js, alongside a sleek hero slider. You can enhance the experience further by adding animations, hover effects, and mobile responsiveness.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment