Home Web Front-end JS Tutorial HTML, CSS and jQuery: Techniques for achieving image enlargement effects

HTML, CSS and jQuery: Techniques for achieving image enlargement effects

Oct 25, 2023 am 09:58 AM
css jquery html

HTML, CSS and jQuery: Techniques for achieving image enlargement effects

HTML, CSS and jQuery: Tips for realizing image enlargement effects

In web design, pictures are a very important element, and achieving image enlargement effects can be Add more visual appeal to the web page. This article will introduce how to use HTML, CSS and jQuery to achieve image magnification effects, and give specific code examples.

1. HTML structure

First, we need to specify the structure of the image to be enlarged in HTML. A simple image enlargement effect usually requires a container containing an image that will be enlarged and show more detail when the mouse is hovered over the image.

HTML code example:

<div class="image-container">
    <img class="zoom-image lazy"  src="/static/imghw/default1.png"  data-src="image.jpg"  alt="image">
</div>
Copy after login

In the above code, we use a <div> element as a container for the image, which contains a The <img src="/static/imghw/default1.png" data-src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" class="lazy" alt="HTML, CSS and jQuery: Techniques for achieving image enlargement effects" > element is used to display images. image.jpg is the address of the image, replace it according to the actual situation.

2. CSS Style

Next, we need to use CSS to style the image, including setting the size of the container, hiding the enlarged image, and adding transition effects.

CSS code example:

.image-container {
    position: relative;
    width: 300px;
    height: 200px;
    overflow: hidden;
}

.zoom-image {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.3s ease;
    transform-origin: center center;
    transform: scale(1);
}

.image-container:hover .zoom-image {
    transform: scale(1.2);
}
Copy after login

In the above code, we define the display size of the enlarged image by setting the width and height of the container. Use the overflow: hidden attribute to hide the portion that exceeds the container size. Through the object-fit: cover attribute, you can maintain the aspect ratio of the image while covering the entire container. transition: transform 0.3s easeThe attribute sets a transition effect so that the image has a smooth animation effect when it is enlarged. transform-origin: center centerThe attribute sets the center point of the image enlargement as the center point of the container. Finally, transform: scale(1) sets the default size of the image, and transform: scale(1.2) makes the image larger on mouse hover.

3. jQuery processing

Based on the above HTML and CSS, we can use jQuery to add some interactive effects, such as adding a magnifying glass effect.

First, introduce the jQuery library into the page:

<script></script>
Copy after login

Next, we need to use jQuery to capture the position of the mouse and move the position of the magnifying glass based on its relative position.

jQuery code example:

$(document).ready(function() {
    $(".image-container").mousemove(function(e) {
        var offset = $(this).offset();
        var xPos = e.pageX - offset.left;
        var yPos = e.pageY - offset.top;

        $(".zoom-image").css({
            "transform-origin": xPos + "px " + yPos + "px"
        });
    });
});
Copy after login

The above code binds mouse movement events by using the .mousemove() method. e.pageX and e.pageY represent the X and Y coordinates of the mouse on the page respectively. By getting the offset of the container, combined with the relative position of the mouse, the position of the magnifying glass can be calculated. Finally, by setting the transform-origin attribute, the magnifying glass position changes as the mouse moves.

To sum up, through the synergy of HTML, CSS and jQuery, we can achieve a simple image enlargement effect. I hope the code examples in this article can be helpful to readers in implementing image enlargement effects in web design. Of course, readers can also modify and expand the code according to their own needs and creativity to achieve more personalized effects.

The above is the detailed content of HTML, CSS and jQuery: Techniques for achieving image enlargement effects. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

React's Role in HTML: Enhancing User Experience React's Role in HTML: Enhancing User Experience Apr 09, 2025 am 12:11 AM

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

How to view the date of bootstrap How to view the date of bootstrap Apr 07, 2025 pm 03:03 PM

Answer: You can use the date picker component of Bootstrap to view dates in the page. Steps: Introduce the Bootstrap framework. Create a date selector input box in HTML. Bootstrap will automatically add styles to the selector. Use JavaScript to get the selected date.

See all articles