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

HTML, CSS and jQuery: Tips for achieving image flip effects

Oct 27, 2023 pm 06:56 PM
css html Image flip

HTML, CSS and jQuery: Tips for achieving image flip effects

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

In modern web design, in order to increase the interactivity and attractiveness of the page, some elements are often added. Special effects to enhance user experience. Among them, the picture flipping effect is a common and eye-catching effect that can make static pictures vivid and interesting.

In this article, we will introduce how to use HTML, CSS and jQuery to achieve image flipping effects, and provide specific code examples for readers' reference.

1. Preparation

First, we need to prepare some picture materials and create a container for them.

<div class="flip-container">
  <div class="flipper">
    <div class="front">
      <img src="/static/imghw/default1.png"  data-src="image1.jpg"  class="lazy" alt="Image 1">
    </div>
    <div class="back">
      <img src="/static/imghw/default1.png"  data-src="image2.jpg"  class="lazy" alt="Image 2">
    </div>
  </div>
</div>

In the above code, we created a div named flip-container as the wrapping container, and created two divs in it: flipper and front and back. Flipper is responsible for controlling the flip effect, while front and back are used to accommodate the pictures that need to be flipped.

2.CSS style settings

Next, we need to add some CSS styles to these elements to achieve the flip effect.

.flip-container {
  perspective: 1000px;
  width: 200px;
  /* 设置适当的宽度和高度 */
  height: 200px;
}

.flipper {
  position: relative;
  transform-style: preserve-3d;
  transition: transform 0.6s;
  transform-origin: 50% 50%;
  /* 根据需要调整翻转速度和翻转位置 */
  width: 200px;
  height: 200px;
}

.front, .back {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  backface-visibility: hidden;
}

.front {
  z-index: 1;
  /* 设置适当的背景颜色或样式 */
  background-color: #fff;
}

.back {
  transform: rotateY(180deg);
  /* 设置需要翻转的图像 */
}

.flip-container:hover .flipper {
  transform: rotateY(180deg);
}

In the above code, we first set the perspective attribute for flip-container to provide a perspective effect and increase realism. Then, we set various CSS properties of flipper to support the 3D flip effect. The front and back elements are used to display the front and back pictures respectively, and hide the back elements through the backface-visibility attribute.

3.jQuery script to implement animation effects

Finally, we use jQuery to create animation effects so that the image can be flipped when the mouse is hovered.

$(document).ready(function() {
  $('.flip-container').hover(function() {
    $(this).find('.flipper').addClass('flip');
  }, function() {
    $(this).find('.flipper').removeClass('flip');
  });
});

In the above code, we use the .hover() function to add a mouse hover event to the .flip-container. When the mouse is hovered over the container, jQuery will dynamically add the .flip class to trigger the flip effect in CSS. When the mouse leaves the container, jQuery will remove the .flip class to achieve the restoration effect of the element.

Summary

Through the combination of HTML, CSS and jQuery, we can easily achieve image flipping effects. By adjusting various properties in CSS, we can obtain flip effects with different styles and effects. Using jQuery to handle mouse events can further improve the user interaction experience.

I hope the tips and code examples provided in this article will be helpful to readers and make it easier and more enjoyable to implement image flipping effects.

The above is the detailed content of HTML, CSS and jQuery: Tips for achieving image flip 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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

PHP Tutorial
1535
276
How to change the list style in CSS How to change the list style in CSS Aug 17, 2025 am 10:04 AM

To change the CSS list style, first use list-style-type to change the bullet or numbering style. 1. Use list-style-type to set the bullet of ul to disc, circle or square, and the number of ol is decimal, lower-alpha, upper-alpha, lower-roman or upper-roman. 2. Remove the tag completely with list-style:none. 3. Use list-style-image:url('bullet.png') to replace it with a custom image. 4. Use list-style-position:in

Why is my HTML image not showing up? Why is my HTML image not showing up? Aug 16, 2025 am 10:08 AM

First, check whether the src attribute path is correct, and ensure that the relative or absolute path matches the HTML file location; 2. Verify whether the file name and extension are spelled correctly and case-sensitive; 3. Confirm that the image file actually exists in the specified directory; 4. Use appropriate alt attributes and ensure that the image format is .jpg, .png, .gif or .webp widely supported by the browser; 5. Troubleshoot browser cache issues, try to force refresh or directly access the image URL; 6. Check server permission settings to ensure that the file can be read and not blocked; 7. Verify that the img tag syntax is correct, including the correct quotes and attribute order, and finally troubleshoot 404 errors or syntax problems through the browser developer tool to ensure that the image is displayed normally.

How to create a dotted border in CSS How to create a dotted border in CSS Aug 15, 2025 am 04:56 AM

Use CSS to create dotted borders, just set the border attribute to dotted. For example, "border:3pxdotted#000" can add a 3-pixel-wide black dot border to the element. By adjusting the border-width, the size of the point can be changed. The wider borders produce larger points. You can set dotted borders for a certain side, such as "border-top:2pxdottedred". Dotted borders are suitable for block-level elements such as div and input. They are often used in focus states or editable areas to improve accessibility. Pay attention to color contrast. At the same time, different from dashed's short-line style, dotted presents a circular dot shape. This feature is widely used in all mainstream browsers.

How to use the address tag in HTML How to use the address tag in HTML Aug 15, 2025 am 06:24 AM

Thetagisusedtodefinecontactinformationfortheauthororownerofadocumentorsection;1.Useitforemail,physicaladdress,phonenumber,orwebsiteURLwithinanarticleorbody;2.Placeitinsideforauthorcontactorinfordocument-widecontact;3.StyleitwithCSSasneeded,notingdefa

How to use the bdo tag to override text direction in HTML How to use the bdo tag to override text direction in HTML Aug 16, 2025 am 09:32 AM

Thebdotagisusedtooverridethebrowser’sdefaulttextdirectionrenderingwhendealingwithmixedleft-to-rightandright-to-lefttext,ensuringcorrectvisualdisplaybyforcingaspecificdirectionusingthedirattributewithvalues"ltr"or"rtl",asdemonstrat

How to change the cursor in CSS How to change the cursor in CSS Aug 16, 2025 am 05:00 AM

Usebuilt-incursortypeslikepointer,help,ornot-allowedtoprovideimmediatevisualfeedbackfordifferentinteractiveelements.2.ApplycustomcursorimageswiththecursorpropertyusingaURL,optionallyspecifyingahotspotandalwaysincludingafallbacklikeautoorpointer.3.Fol

How to use the ::before and ::after pseudo-elements in CSS How to use the ::before and ::after pseudo-elements in CSS Aug 14, 2025 pm 08:25 PM

::before and ::after pseudo-elements must insert content through the content attribute, and even if they are empty, they must be defined; 2. They are often used to add decorative icons, tooltips, or clear floating, and generate visual effects through CSS; 3. Positioning and style control can be combined with attributes such as position and display, and supports animation and transformation; 4. It should be avoided to use directly on empty elements such as img or input, and the generated content cannot be selected or read by screen readers, so it is not used for displaying key information.

How to use the grid-auto-flow property in CSS How to use the grid-auto-flow property in CSS Aug 14, 2025 pm 09:26 PM

The default value of grid-auto-flow is row, which means to be arranged in row order, and create a new row when the row is full; 2. When set to column, prioritize the column. Create a new column after the column is full, and it needs to be used with grid-auto-columns; 3. Adding the density keyword can enable tight arrangement, trying to fill the gaps in front, but it may affect accessibility; this property only affects grid items that are not explicitly positioned, and should be used in combination with grid-auto-rows or grid-auto-columns to control the implicit track size, and is suitable for automatic arrangement in dynamic or responsive layouts.

See all articles