Method and device for forming image magnification special effect based on React mouse hover

By creating ImageItem and Accordion components in React, combined with useState and CSS Flexbox, the performance and compatibility issues of mouse-over image magnification effects are solved, achieving high-performance and good user experience image magnification effects, ensuring the clarity of the image and consistency.

CN118466806BActive Publication Date: 2024-11-08ONE NETWORK INTEROPERABILITY (BEIJING) TECH CO LTD
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202410916776.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-07-10
Publication Date
2024-11-08
Estimated Expiration
2044-07-10

AI Technical Summary

Technical Problem

In the prior art, the mouse hover image enlargement special effects have problems in performance, compatibility, responsive design, image distortion and event conflict, resulting in performance bottlenecks, inconsistent performance, poor user experience and animation effects interference.

Method used

Using a React-based approach, we create ImageItem components and Accordion components, use the useState hook to manage hover state, combine CSS Flexbox and styled-components to achieve image amplification effect, and improve responsiveness and performance through media query and performance optimization technologies.

Benefits of technology

It realizes the mouse hover image magnification accordion effect with high performance and good user experience in various devices and browser environments, solving performance bottlenecks, compatibility and user experience issues, and ensuring the clarity and consistency of pictures.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118466806B_ABST
    Figure CN118466806B_ABST
Patent Text Reader

Abstract

The embodiment of the present invention provides a method and device for forming a picture magnification special effect based on the hovering of a React mouse, belonging to the field of image processing technology, the method comprising: creating an ImageItem component, the ImageItem component receiving a src attribute for rendering a picture; creating an Accordion component, the Accordion component including a plurality of ImageItem subcomponents; defining a basic style of the picture in the ImageItem component, dynamically applying a hovering style through a hovered state; when it is determined that the current picture is in a hovering state, magnifying the picture, and adjusting the width of the picture container to achieve a magnification effect of the picture. This solution can improve the special effect performance of the picture and the compatibility of the picture special effects in different browsers.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of image processing, and in particular to a method, a device and an electronic device for forming a picture magnification special effect based on React mouse hovering. Background Art

[0002] Mouse hover image zoom means that when the user's mouse pointer hovers over an image on a web page or application, the image will automatically zoom in to provide a more detailed view without the user having to do any additional work (such as clicking or scrolling). This effect is usually achieved through CSS (Cascading Style Sheets) and HTML (Hypertext Markup Language), and can also be combined with JavaScript to achieve more complex interactions.

[0003] By enlarging the image by hovering the mouse, you can create an accordion image effect (Accordion Image Hover Effect). The accordion image effect is a web design technique in which when the user hovers the mouse over an image, the image will expand or enlarge, and the adjacent images will shrink or hide accordingly, to simulate the effect of the bellows opening and closing when an accordion is played.

[0004] The accordion effect can not only enhance the visual effect and interactivity of the web page, but also help users better browse and understand the page content.

[0005] The existing technology has the following main problems:

[0006] 1. Performance issues:

[0007] Frequent zooming in and out animations may cause performance bottlenecks, especially when there are many images or the image resolution is high, which may cause page lag and frame drops.

[0008] 2. Compatibility issues:

[0009] Different browsers have different levels of support for CSS animations and JavaScript, which may cause special effects to behave inconsistently in some browsers or even fail to work properly.

[0010] 3. Responsive Design:

[0011] When implementing this special effect on mobile devices, the difference between touch events and mouse events may result in a poor user experience, and additional processing of touch events is required to ensure a consistent user experience.

[0012] 4. Image distortion:

[0013] When images are enlarged, they may become distorted or blurred, especially in the case of non-proportional enlargement. The images need to be processed to ensure their clarity and quality after enlargement.

[0014] 5. Event Conflict:

[0015] In an accordion component, event handling of multiple elements may interfere with each other, resulting in unexpected behavior. For example, moving the mouse quickly may trigger multiple animations, causing overlapping or conflicting animation effects.

[0016] The above problems become technical problems that need to be solved. Summary of the invention

[0017] In view of this, the embodiments of the present invention provide a method, device and electronic device for forming a picture magnification special effect based on React mouse hovering, which at least partially solve the problems existing in the prior art.

[0018] In a first aspect, an embodiment of the present invention provides a method for forming a picture magnification special effect based on a React mouse hover, comprising:

[0019] Create an ImageItem component, which receives the src attribute for rendering an image. Use the useState hook in the ImageItem component to manage a Boolean state hovered to indicate whether the current image is in a hover state.

[0020] Create an Accordion component, which contains multiple ImageItem subcomponents. Use CSS Flexbox layout to arrange the ImageItem components horizontally. Define an array of image paths in the Accordion component, and use the map method of the array to dynamically render multiple ImageItem components.

[0021] Define the basic style of the image in the ImageItem component, dynamically apply the hover style through the hovered state, and use the CSS transition property to define the transition effect of zooming in and out of the image;

[0022] In the ImageItem component, use the onMouseEnter and onMouseLeave event handlers to update the hovered state. Use the style attributes in styled-components to apply different styles according to the hovered state. When it is determined that the current image is in the hovered state, enlarge the image and adjust the width of the image container to achieve the effect of enlarging the image.

[0023] According to a specific implementation of the embodiment of the present disclosure, before creating the ImageItem component, the method further includes:

[0024] Create a new React project using the create-react-app command;

[0025] Install styled-components to facilitate handling of styles within components.

[0026] According to a specific implementation of the embodiment of the present disclosure, the method further includes:

[0027] Import and render the Accordion component in App.js;

[0028] In App.js, add necessary container styles to the Accordion component to ensure that the component layout and display effects meet expectations.

[0029] According to a specific implementation of the embodiment of the present disclosure, the creating of the ImageItem component includes:

[0030] Set the src attribute for the ImageItem component. The src attribute will be used to specify the URL of the image to be displayed.

[0031] Inside the ImageItem component, use React's useState hook to create a state variable called hovered to track whether the user's mouse is hovering over the image;

[0032] Add onMouseEnter and onMouseLeave event handlers to the image element. When the user's mouse enters the image area, set the hovered state to true; when the mouse leaves, set it to false.

[0033] Use the img tag to render the image and set the src attribute to the image URL passed in via props.

[0034] According to a specific implementation of the embodiment of the present disclosure, the creating of the Accordion component includes:

[0035] Define an array of image paths inside the Accordion component, use the map method to traverse the array of image paths, create an ImageItem component instance for each path, use CSS Flexbox layout to arrange the ImageItem components horizontally, and add media queries in CSS to adapt to different screen sizes;

[0036] Use Flexbox layout to arrange ImageItem components horizontally and set preset spacing. In responsive design, use media queries to change the layout.

[0037] The Accordion component obtains the state of the ImageItem through React's callback function Context API. In the ImageItem component, the callback function is passed using props.

[0038] Set the image processing function in the Accordion component. When the number of images exceeds the preset value, the performance of the images will be optimized.

[0039] According to a specific implementation of the embodiment of the present disclosure, the image processing function is set in the Accordion component, and when the number of images exceeds a preset value, the performance optimization processing is performed on the images, including:

[0040] Select each image element in multiple images as an independent scroll container, and set the scroll area size of the scroll container;

[0041] Measure the height of each image item, and calculate the visible area of ​​the image at the current scroll position based on the visible area of ​​the scroll container and the size of the image item;

[0042] Only render the currently visible images, and recycle the images that have been scrolled out of the viewport and are no longer visible;

[0043] Set a placeholder for each image item, which is a low-resolution loading indicator;

[0044] Use or React's listening function to monitor the distance of each image item from the top of the viewport;

[0045] When the image enters the available area, start loading the corresponding high-definition image. When the high-definition image is loaded, replace the corresponding placeholder with it.

[0046] If the image fails to load, show an error message and fall back to the default image.

[0047] According to a specific implementation of the embodiment of the present disclosure, the basic style of the image is defined in the ImageItem component, the hover style is dynamically applied through the hovered state, and the transition effect of zooming in and out of the image is defined using the CSS transition attribute, including:

[0048] Define the basic image style class and the hover style class. The basic image style class is used to describe the basic image style, and the hover style class is used to describe the hover style.

[0049] The basic image style class contains the basic image style settings, including width, height, and border;

[0050] The hover style class contains the style settings for images when they are hovered, especially the magnification effect and transition effect. The magnification effect uses the scale() function in the transform attribute of CSS to enlarge the image, and the transition effect uses the transition attribute of CSS to define the transition effect, including the transition properties, transition duration and transition easing function.

[0051] According to a specific implementation of the embodiment of the present disclosure, the onMouseEnter and onMouseLeave event processing functions are used to update the hovered state, the style attributes in styled-components are used to apply different styles according to the hovered state, and when it is determined that the current image is in the hovering state, the image is enlarged and the width of the image container is adjusted, including:

[0052] Use the styled-components library to create two styled components: ImageContainer and StyledImage.

[0053] In the style of ImageContainer, the width of the container is dynamically set according to the hovered state. When hovered is true, the first width value is set to display the enlarged image. When hovered is false, it is set to the default width.

[0054] In the StyledImage style, dynamically adjust the image magnification ratio according to the hovered state, and use the transform: scale() property of CSS to achieve the image magnification effect. When hovered is true, set a zoom ratio greater than 1. When hovered is false, keep the original ratio or set it to 1.

[0055] Add onMouseEnter and onMouseLeave event handling functions to the ImageContainer component;

[0056] When the mouse enters the image container, the first update function is called to update the hovered state to true, thereby triggering a style change, enlarging the image and adjusting the container width;

[0057] When the mouse leaves the image container, the second update function is called to update the hovered state to false, restoring the image and container to their original state;

[0058] In the rendering part of the React component, use ImageContainer to wrap StyledImage, pass the source path src of the image as a property to StyledImage, and ensure that the hovered state is passed as a property to the image container ImageContainer and the image style StyledImage, so that the image container ImageContainer and the image style StyledImage dynamically adjust the style according to the hovered state.

[0059] In a second aspect, an embodiment of the present invention provides a device for forming a picture magnification special effect based on a React mouse hover, comprising:

[0060] The first creation module creates an ImageItem component, which receives a src attribute for rendering an image, and uses a useState hook in the ImageItem component to manage a Boolean state hovered, which is used to indicate whether the current image is in a hovering state;

[0061] The second creation module creates an Accordion component, which contains multiple ImageItem subcomponents. The ImageItem components are arranged horizontally using CSS Flexbox layout. An array of image paths is defined in the Accordion component, and the map method of the array is used to dynamically render multiple ImageItem components.

[0062] Define the module, define the basic style of the image in the ImageItem component, dynamically apply the hover style through the hovered state, and use the CSS transition property to define the transition effect of zooming in and out of the image;

[0063] In the zoom module, in the ImageItem component, the onMouseEnter and onMouseLeave event processing functions are used to update the hovered state. The style attributes in styled-components are used to apply different styles according to the hovered state. When it is determined that the current image is in the hovering state, the image is zoomed in and the width of the image container is adjusted to achieve the image zooming effect.

[0064] In a third aspect, an embodiment of the present invention further provides an electronic device, the electronic device comprising:

[0065] at least one processor; and,

[0066] a memory communicatively connected to the at least one processor; wherein,

[0067] The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the method for forming a picture enlargement special effect based on React mouse hover in any of the aforementioned first aspects or any of the implementations of the first aspect.

[0068] In a fourth aspect, an embodiment of the present invention further provides a non-transitory computer-readable storage medium, which stores computer instructions, and the computer instructions are used to enable the computer to execute the method for forming a picture enlargement special effect based on React mouse hover in the aforementioned first aspect or any implementation of the first aspect.

[0069] In a fifth aspect, an embodiment of the present invention further provides a computer program product, which includes a computer program stored on a non-transitory computer-readable storage medium, and the computer program includes program instructions. When the program instructions are executed by a computer, the computer executes the method for forming a picture enlargement special effect based on React mouse hover in the aforementioned first aspect or any implementation of the first aspect.

[0070] The solution for forming a picture magnification special effect based on the hovering of the mouse of React in the embodiment of the present invention includes: creating an ImageItem component, the ImageItem component receives a src attribute for rendering a picture, and uses the useState hook in the ImageItem component to manage a Boolean state hovered, which is used to indicate whether the current picture is in a hovering state; creating an Accordion component, the Accordion component includes multiple ImageItem subcomponents, and uses the CSSFlexbox layout to arrange the ImageItem components horizontally, defines an array of picture paths in the Accordion component, and uses the map method of the array to dynamically render multiple ImageItem components; defines the basic style of the picture in the ImageItem component, dynamically applies the hovering style through the hovered state, and uses the CSS transition attribute to define the transition effect of zooming in and out of the picture; in the ImageItem component, uses the onMouseEnter and onMouseLeave event processing functions to update the hovered state, uses the style attributes in the styled-components, applies different styles according to the hovered state, and when it is determined that the current picture is in a hovering state, zooms in the picture, and adjusts the width of the picture container to achieve the picture zooming effect. The present invention realizes an accordion effect of enlarging a picture when the mouse is hovering, which has high performance and good user experience in various equipment and browser environments. BRIEF DESCRIPTION OF THE DRAWINGS

[0071] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0072] Figure 1 A schematic flow chart of a method for forming a picture magnification special effect based on a React mouse hover provided by an embodiment of the present invention;

[0073] Figure 2 A schematic flow chart of another method for forming a picture magnification special effect based on a React mouse hover provided by an embodiment of the present invention;

[0074] Figure 3 A schematic diagram of the structure of a device for forming a picture magnification special effect based on a React mouse hover provided in an embodiment of the present invention;

[0075] Figure 4A schematic diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0076] The embodiments of the present invention are described in detail below with reference to the accompanying drawings.

[0077] The following describes the embodiments of the present disclosure through specific examples, and those skilled in the art can easily understand other advantages and effects of the present disclosure from the contents disclosed in this specification. Obviously, the described embodiments are only a part of the embodiments of the present disclosure, rather than all of the embodiments. The present disclosure can also be implemented or applied through other different specific embodiments, and the details in this specification can also be modified or changed in various ways based on different viewpoints and applications without departing from the spirit of the present disclosure. It should be noted that the following embodiments and features in the embodiments can be combined with each other without conflict. Based on the embodiments in the present disclosure, all other embodiments obtained by ordinary technicians in the field without making creative work are within the scope of protection of the present disclosure.

[0078] It should be noted that various aspects of the embodiments within the scope of the appended claims are described below. It should be apparent that the aspects described herein may be embodied in a wide variety of forms, and any specific structure and / or function described herein is merely illustrative. Based on the present disclosure, it should be understood by those skilled in the art that an aspect described herein may be implemented independently of any other aspect, and two or more of these aspects may be combined in various ways. For example, any number of aspects described herein may be used to implement the device and / or practice the method. In addition, other structures and / or functionalities other than one or more of the aspects described herein may be used to implement this device and / or practice this method.

[0079] It should also be noted that the illustrations provided in the following embodiments are only schematic illustrations of the basic concept of the present disclosure. The drawings only show components related to the present disclosure rather than being drawn according to the number, shape and size of components in actual implementation. In actual implementation, the type, quantity and proportion of each component may be changed arbitrarily, and the component layout may also be more complicated.

[0080] Additionally, in the following description, specific details are provided to facilitate a thorough understanding of the examples. However, it will be understood by those skilled in the art that the aspects described may be practiced without these specific details.

[0081] The disclosed embodiment provides a method for forming a special effect of image magnification based on React mouse hovering. The method for forming a special effect of image magnification based on React mouse hovering provided in this embodiment can be executed by a computing device, which can be implemented as software, or as a combination of software and hardware, and the computing device can be integrated in a server, terminal device, etc.

[0082] See also Figure 1 and Figure 2 The embodiment of the present disclosure provides a method for forming a picture magnification special effect based on a React mouse hover, comprising:

[0083] S101, creating an ImageItem component, the ImageItem component receiving a src attribute for rendering an image, and using a useState hook in the ImageItem component to manage a Boolean state hovered, which is used to indicate whether the current image is in a hovering state.

[0084] To create an ImageItem component, follow these steps:

[0085] First, define a React function component named ImageItem. The ImageItem component can receive a src attribute, which will be used to specify the URL of the image to be displayed.

[0086] Inside the component, you'll use React's useState hook to create a state variable called hovered. This variable is a Boolean value that tracks whether the user's mouse is hovering over the image.

[0087] You need to add onMouseEnter and onMouseLeave event handlers to the image element. When the user's mouse enters the image area, set the hovered state to true; when the mouse leaves, set it to false.

[0088] Depending on the value of the hovered state, you can change the image's style or behavior to reflect its hover state. For example, you could change the image's border color or display a tooltip.

[0089] Finally, we render the image using the img tag and set the src attribute to the image URL passed in via props.

[0090] S102, creating an Accordion component, wherein the Accordion component includes multiple ImageItem subcomponents, arranging the ImageItem components horizontally using CSS Flexbox layout, defining an array of image paths in the Accordion component, and dynamically rendering multiple ImageItem components using the map method of the array.

[0091] You can define an Accordion component, define an array of image paths inside the component, use the map method to traverse the array of image paths, and create an ImageItem component instance for each path.

[0092] Use CSS Flexbox layout to arrange the ImageItem components horizontally. If a responsive layout is required, you can add media queries in CSS to adapt to different screen sizes.

[0093] Use Flexbox layout to arrange the ImageItem components horizontally and set appropriate spacing. Add hover effect styles to the ImageItem components as needed. In responsive design, you can use media queries to change the layout (such as stacking images on small screens).

[0094] If the Accordion component needs to know the state of an ImageItem (for example, which one is clicked or hovered), it can be implemented through callback functions or React's Context API. In the ImageItem component, these callback functions can be passed using props and called when needed. If there are a lot of images, you can consider using react-window to avoid rendering all images at once, thereby improving performance. You can also optimize image loading, such as using react-lazyload to load images only when they are about to enter the viewport.

[0095] If the Accordion component is interactive (such as clickable or keyboard navigable), you need to ensure that it meets accessibility requirements. This may include adding appropriate alt text to images, supporting keyboard navigation and focus management, etc.

[0096] When an image fails to load, an error message or fallback image should be displayed. While the image is loading, a loading indicator can be displayed. When designing components with future extensibility in mind, you can add properties to support custom image size, margins, styles, etc. You can also add more interactive features, such as opening a modal window or jumping to another page when clicking on the image.

[0097] When dealing with a large number of images and performance optimization is a concern, it is indeed possible to reduce the burden on the browser through performance optimization techniques. Here are the specific steps to implement these techniques:

[0098] Determine the container and scrolling area: First, determine which element will be the scrolling container and the size of the scrolling area of ​​this container. Measure the height (or width, if it is horizontal scrolling) of each image item. Based on the visible area of ​​the scrolling container and the size of the image items, calculate which image items are visible at the current scroll position.

[0099] Only render the currently visible image items, and recycle (i.e. unload) those image items that have been scrolled out of the viewport and are no longer visible. This can greatly reduce the number of DOM elements and improve performance.

[0100] When a scroll event is triggered, recalculate which image items are visible and update the rendered list. When the image items are of different sizes, more complex logic may be required to ensure smoothness and accuracy when scrolling.

[0101] Set up a placeholder for each image item, usually a low-resolution loading indicator or a default image. Use JavaScript (or React-specific hooks) to monitor each image item's distance from the top of the viewport.

[0102] When an image item is about to enter the viewport (for example, the distance from the top of the viewport is less than a threshold), start loading the corresponding high-definition image. When the high-definition image is loaded, it replaces the corresponding placeholder. If the image fails to load, display an error prompt or fall back to another default image.

[0103] S103, define the basic style of the image in the ImageItem component, dynamically apply the hover style through the hovered state, and use the CSS transition attribute to define the transition effect of zooming in and out of the image.

[0104] You can define two CSS classes: one for the base style of the image, and another for the hover style. In the hover style, you will use the transform property to enlarge the image, and the transition property to define the transition effect. In the render function of the ImageItem component, you can dynamically apply styles based on the hovered state.

[0105] You need to listen to the mouseenter and mouseleave events to update the hovered state. For touch devices, you also need to listen to the touchstart and touchend (or touchmove) events to simulate the hover effect.

[0106] Since you may need to listen to multiple events, make sure to clean up these event listeners when the component is uninstalled to avoid memory leaks. You can also use debounce or throttle techniques to optimize the execution frequency of event processing functions, especially in high-frequency touch events.

[0107] In addition to zooming in and out, you can also add other interactive effects, such as changing the brightness, contrast, or saturation of the image. These can be achieved through the filter property of CSS. Consider using media queries to create responsive designs so that the ImageItem component can work well on different screen sizes and devices.

[0108] If you want your ImageItem component to be accessible to all users, including those using screen readers or keyboard navigation, you can add appropriate ARIA attributes to enhance accessibility.

[0109] The basic image style class contains the basic image style settings, such as width, height, border, etc.

[0110] The width can be set to a fixed value or percentage to suit your layout needs. The height can be set to auto to keep the original aspect ratio of the image, or set to a fixed value. If necessary, you can add a border to the image and set the border style, color, and width. Other possible basic style properties include margin, padding, box-shadow, etc.

[0111] The hover style class contains the style settings for images when they are hovered (when the mouse is over the image), especially the magnification effect and transition effect. The magnification effect (transform: scale()) can use the scale() function in the transform property of CSS to magnify the image. For example, scale(1.1) will magnify the image to 110% of its original size. The transition effect (transition) can use the transition property of CSS to define the transition effect, including the transition properties (such as transform), the transition duration (such as 0.3s), and the transition easing function (such as ease-in-out).

[0112] S104, in the ImageItem component, use the onMouseEnter and onMouseLeave event processing functions to update the hovered state, use the style attributes in the styled-components to apply different styles according to the hovered state, and when it is determined that the current image is in the hovering state, enlarge the image and adjust the width of the image container to achieve the effect of enlarging the image.

[0113] You can use the styled-components library to create two styled components: one is the image container (such as ImageContainer), and the other is the image itself (such as StyledImage).

[0114] In the style of ImageContainer, the width of the container is dynamically set according to the hovered state. When hovered is true, a larger width value is set to display the enlarged image; when hovered is false, a smaller default width is set.

[0115] In the StyledImage style, the image magnification ratio is also dynamically adjusted according to the hovered state. Use the CSS transform: scale() property to achieve the image magnification effect. When hovered is true, set a zoom ratio greater than 1; when hovered is false, keep the original ratio or set it to 1.

[0116] Add onMouseEnter and onMouseLeave event handlers to the ImageContainer component. When the mouse enters the image container (the onMouseEnter event is triggered), call a function to update the hovered state to true, thereby triggering a style change, enlarging the image and adjusting the container width.

[0117] When the mouse leaves the image container (the onMouseLeave event is triggered), another function is called to update the hovered state to false, restoring the image and container to their original state.

[0118] In the rendering part of the React component, wrap the StyledImage with ImageContainer. Pass the source path (src) of the image as a property to the StyledImage, and make sure the hovered state is passed as a property to both styled components so that they can dynamically adjust their styles based on that state.

[0119] See also Figure 2The embodiment of the present invention also provides another implementation method, specifically including:

[0120] S201, project initialization and environment setup.

[0121] a) Initialize a React project: Use the `create-react-app` command to create a new React project.

[0122] b) Install dependencies: Install `styled-components` to facilitate the handling of styles within components.

[0123] S202, create a picture component.

[0124] a) Component definition: Create a component named ImageItem, which receives the src attribute of the image and is used to render the image.

[0125] b) State management: Use the useState hook in the component to manage a Boolean state hovered, which is used to indicate whether the current image is in a hover state.

[0126] c) Event handling:

[0127] i.`onMouseEnter` event: When the mouse hovers over the image, set the `hovered` state to `true`.

[0128] ii.`onMouseLeave` event: When the mouse leaves the image, set the `hovered` state to `false`.

[0129] d) Style processing: Use `styled-components` to define the style of the image in different states:

[0130] i. Default state: Set the basic width and height of the image.

[0131] ii. Hover state: Use `transform: scale` or adjust the image width to achieve the zoom effect, and use CSS transition effects to make the zoom process smoother.

[0132] S203, create an accordion component.

[0133] a) Component definition: Create a component named `Accordion`, which contains multiple `ImageItem` subcomponents.

[0134] b) Layout management: Use CSS Flexbox layout to arrange the ImageItem components horizontally.

[0135] c) Image data: Define an array of image paths in the Accordion component, and use the array's map method to dynamically render multiple ImageItem components.

[0136] S204, style processing.

[0137] a) Basic style: Define the basic style of the image in the ImageItem component, including width, height, and margins.

[0138] b) Hover styles: Dynamically apply hover styles through the `hovered` state, such as increasing the image width or enlarging the image using transform: scale.

[0139] c) Transition effect: Use CSS transition properties to define the transition effect of image zooming in and out to make the animation smoother.

[0140] S205, event processing.

[0141] a) Mouse events:

[0142] i. In the `ImageItem` component, use the `onMouseEnter` and `onMouseLeave` event handlers to update the `hovered` state.

[0143] ii. Dynamically apply different styles by judging the `hovered` state.

[0144] b) Dynamic style:

[0145] i. Use style properties in `styled-components` to apply different styles based on the `hovered` state.

[0146] ii. When hovering, enlarge the image and adjust the width of the image container to achieve an accordion effect.

[0147] S206, integrating the accordion component into the main application.

[0148] a) Main application component: Import and render the Accordion component in App.js.

[0149] b) Style adjustment: Add necessary container styles to the Accordion component in App.js to ensure that the component layout and display effects meet expectations.

[0150] S207, testing and debugging.

[0151] a) Functional testing: Run the project in a browser to test the functionality of the accordion effect, ensuring that each image can be enlarged correctly when hovering and restored to its original state when the mouse leaves.

[0152] b) Performance tuning: Check the smoothness of the animation and ensure there is no lag.

[0153] c) Compatibility testing: Test the performance of components in different browsers to ensure compatibility.

[0154] According to a specific implementation of the embodiment of the present disclosure, before creating the ImageItem component, the method further includes:

[0155] Create a new React project using the create-react-app command;

[0156] Install styled-components to facilitate handling of styles within components.

[0157] According to a specific implementation of the embodiment of the present disclosure, the method further includes:

[0158] Import and render the Accordion component in App.js;

[0159] In App.js, add necessary container styles to the Accordion component to ensure that the component layout and display effects meet expectations.

[0160] According to a specific implementation of the embodiment of the present disclosure, the creating of the ImageItem component includes:

[0161] Set the src attribute for the ImageItem component. The src attribute will be used to specify the URL of the image to be displayed.

[0162] Inside the ImageItem component, use React's useState hook to create a state variable called hovered to track whether the user's mouse is hovering over the image;

[0163] Add onMouseEnter and onMouseLeave event handlers to the image element. When the user's mouse enters the image area, set the hovered state to true; when the mouse leaves, set it to false.

[0164] Use the img tag to render the image and set the src attribute to the image URL passed in via props.

[0165] According to a specific implementation of the embodiment of the present disclosure, the creating of the Accordion component includes:

[0166] Define an array of image paths inside the Accordion component, use the map method to traverse the array of image paths, create an ImageItem component instance for each path, use CSS Flexbox layout to arrange the ImageItem components horizontally, and add media queries in CSS to adapt to different screen sizes;

[0167] Use Flexbox layout to arrange the ImageItem components horizontally, use props to pass the callback function column, and set the preset value of the spacing. In responsive design, use media queries to change the layout;

[0168] The Accordion component obtains the state of the ImageItem through React's callback function Context API. In the ImageItem component, the callback function is passed using props.

[0169] Set the image processing function in the Accordion component. When the number of images exceeds the preset value, the performance of the images will be optimized.

[0170] According to a specific implementation of the embodiment of the present disclosure, the image processing function is set in the Accordion component, and when the number of images exceeds a preset value, the performance optimization processing is performed on the images, including:

[0171] Select each image element in multiple images as an independent scroll container, and set the scroll area size of the scroll container;

[0172] Measure the height of each image item, and calculate the visible area of ​​the image at the current scroll position based on the visible area of ​​the scroll container and the size of the image item;

[0173] Only render the currently visible images, and recycle the images that have been scrolled out of the viewport and are no longer visible;

[0174] Set a placeholder for each image item, which is a low-resolution loading indicator;

[0175] Use or React's listening function to monitor the distance of each image item from the top of the viewport;

[0176] When the image enters the available area, start loading the corresponding high-definition image. When the high-definition image is loaded, replace the corresponding placeholder with it.

[0177] If the image fails to load, show an error message and fall back to the default image.

[0178] According to a specific implementation of the embodiment of the present disclosure, the basic style of the image is defined in the ImageItem component, the hover style is dynamically applied through the hovered state, and the transition effect of zooming in and out of the image is defined using the CSS transition attribute, including:

[0179] Define the basic image style class and the hover style class. The basic image style class is used to describe the basic image style, and the hover style class is used to describe the hover style.

[0180] The basic image style class contains the basic image style settings, including width, height, and border;

[0181] The hover style class contains the style settings for images when they are hovered, especially the magnification effect and transition effect. The magnification effect uses the scale() function in the transform attribute of CSS to enlarge the image, and the transition effect uses the transition attribute of CSS to define the transition effect, including the transition properties, transition duration and transition easing function.

[0182] According to a specific implementation of the embodiment of the present disclosure, the onMouseEnter and onMouseLeave event processing functions are used to update the hovered state, the style attributes in styled-components are used to apply different styles according to the hovered state, and when it is determined that the current image is in the hovering state, the image is enlarged and the width of the image container is adjusted, including:

[0183] Use the styled-components library to create two styled components: ImageContainer and StyledImage.

[0184] In the style of ImageContainer, the width of the container is dynamically set according to the hovered state. When hovered is true, the first width value is set to display the enlarged image. When hovered is false, it is set to the default width.

[0185] In the StyledImage style, dynamically adjust the image magnification ratio according to the hovered state, and use the transform: scale() property of CSS to achieve the image magnification effect. When hovered is true, set a zoom ratio greater than 1. When hovered is false, keep the original ratio or set it to 1.

[0186] Add onMouseEnter and onMouseLeave event handling functions to the ImageContainer component;

[0187] When the mouse enters the image container, the first update function is called to update the hovered state to true, thereby triggering a style change, enlarging the image and adjusting the container width;

[0188] When the mouse leaves the image container, the second update function is called to update the hovered state to false, restoring the image and container to their original state;

[0189] In the rendering part of the React component, use ImageContainer to wrap StyledImage, pass the source path src of the image as a property to StyledImage, and ensure that the hovered state is passed as a property to the image container ImageContainer and the image style StyledImage, so that the image container ImageContainer and the image style StyledImage dynamically adjust the style according to the hovered state.

[0190] Corresponding to the above method embodiment, see Figure 3 The embodiment of the present invention further discloses a device 30 for forming a picture magnification special effect based on a React mouse hover, comprising:

[0191] The first creation module 301 creates an ImageItem component, which receives a src attribute for rendering an image, and uses a useState hook in the ImageItem component to manage a Boolean state hovered, which is used to indicate whether the current image is in a hovering state;

[0192] The second creation module 302 creates an Accordion component, wherein the Accordion component includes multiple ImageItem subcomponents, uses CSS Flexbox layout to arrange the ImageItem components horizontally, defines an array of image paths in the Accordion component, and uses the map method of the array to dynamically render multiple ImageItem components;

[0193] Definition module 303, defines the basic style of the image in the ImageItem component, dynamically applies the hover style through the hovered state, and uses the CSS transition attribute to define the transition effect of zooming in and out of the image;

[0194] The magnification module 304, in the ImageItem component, uses the onMouseEnter and onMouseLeave event processing functions to update the hovered state, uses the style attributes in the styled-components, applies different styles according to the hovered state, and when it is determined that the current image is in the hovering state, magnifies the image and adjusts the width of the image container to achieve the image magnification effect.

[0195] See also Figure 4 The embodiment of the present invention further provides an electronic device 60, the electronic device comprising:

[0196] at least one processor; and,

[0197] a memory communicatively connected to the at least one processor; wherein,

[0198] The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the method for forming a picture enlargement special effect based on React mouse hover in the aforementioned method embodiment.

[0199] An embodiment of the present invention further provides a non-transitory computer-readable storage medium, which stores computer instructions, and the computer instructions are used to enable the computer to execute the aforementioned method embodiment.

[0200] An embodiment of the present invention also provides a computer program product, which includes a computer program stored on a non-transitory computer-readable storage medium, and the computer program includes program instructions. When the program instructions are executed by a computer, the computer executes the method for forming a picture enlargement special effect based on React mouse hover in the aforementioned method embodiment.

[0201] Reference below Figure 4 , which shows a schematic diagram of the structure of an electronic device 60 suitable for implementing the embodiment of the present disclosure. The electronic device in the embodiment of the present disclosure may include, but is not limited to, mobile terminals such as mobile phones, notebook computers, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), vehicle terminals (such as vehicle navigation terminals), etc., and fixed terminals such as digital TVs, desktop computers, etc. Figure 4 The electronic device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present disclosure.

[0202] like Figure 4 As shown, the electronic device 60 may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 601, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 602 or a program loaded from a storage device 608 into a random access memory (RAM) 603. In the RAM 603, various programs and data required for the operation of the electronic device 60 are also stored. The processing device 601, the ROM 602, and the RAM 603 are connected to each other via a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.

[0203] Typically, the following devices may be connected to the I / O interface 605: an input device 606 including, for example, a touch screen, a touch pad, a keyboard, a mouse, an image sensor, a microphone, an accelerometer, a gyroscope, etc.; an output device 607 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 608 including, for example, a magnetic tape, a hard disk, etc.; and a communication device 609. The communication device 609 may allow the electronic device 60 to communicate with other devices wirelessly or by wire to exchange data. Although Figure 4 The electronic device 60 is shown with various devices, but it should be understood that it is not required to implement or have all the devices shown. More or fewer devices may be implemented or have instead.

[0204] In particular, according to an embodiment of the present disclosure, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present disclosure includes a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication device 609, or installed from a storage device 608, or installed from a ROM 602. When the computer program is executed by the processing device 601, the above-mentioned functions defined in the method of the embodiment of the present disclosure are executed.

[0205] The above is only a specific embodiment of the present invention, but the protection scope of the present invention is not limited thereto. Any changes or substitutions that can be easily thought of by a person skilled in the art within the technical scope disclosed by the present invention should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the protection scope of the claims.

Claims

1. A method for forming a picture magnification special effect based on React mouse hover, characterized in that: include: Create an ImageItem component, which receives the src attribute for rendering an image. Use the useState hook in the ImageItem component to manage a Boolean state hovered to indicate whether the current image is in a hover state. Create an Accordion component, which contains multiple ImageItem subcomponents. Use CSS Flexbox layout to arrange the ImageItem components horizontally. Define an array of image paths in the Accordion component, and use the map method of the array to dynamically render multiple ImageItem components. Define the basic style of the image in the ImageItem component, dynamically apply the hover style through the hovered state, and use the CSS transition property to define the transition effect of zooming in and out of the image; In the ImageItem component, use the onMouseEnter and onMouseLeave event handlers to update the hovered state. Use the style attributes in styled-components to apply different styles according to the hovered state. When the current image is determined to be in the hovered state, enlarge the image and adjust the width of the image container to achieve the image enlargement effect. The creation of the Accordion component includes: Define an array of image paths inside the Accordion component, use the map method to traverse the array of image paths, create an ImageItem component instance for each path, use CSS Flexbox layout to arrange the ImageItem components horizontally, and add media queries in CSS to adapt to different screen sizes; Use Flexbox layout to arrange ImageItem components horizontally and set preset spacing. In responsive design, use media queries to change the layout. The Accordion component obtains the state of the ImageItem through React's callback function Context API. In the ImageItem component, the callback function is passed using props. Set the image processing function in the Accordion component. When the number of images exceeds the preset value, the performance of the images will be optimized.

2. The method according to claim 1, characterized in that Before creating the ImageItem component, the method further includes: Create a new React project using the create-react-app command; Install styled-components to facilitate handling of styles within components.

3. The method according to claim 2, characterized in that The method further comprises: Import and render the Accordion component in App.js; In App.js, add necessary container styles to the Accordion component to ensure that the component layout and display effects meet expectations.

4. The method according to claim 3, characterized in that The creation of the ImageItem component includes: Set the src attribute for the ImageItem component, where the src attribute is used to specify the URL of the image to be displayed; Inside the ImageItem component, use React's useState hook to create a state variable called hovered to track whether the user's mouse is hovering over the image; Add onMouseEnter and onMouseLeave event handlers to the image element. When the user's mouse enters the image area, set the hovered state to true; when the mouse leaves, set it to false. Use the img tag to render the image and set the src attribute to the image URL passed in via props.

5. The method according to claim 1, characterized in that The image processing function is set in the Accordion component. When the number of images exceeds a preset value, the performance of the images is optimized, including: Select each image element in multiple images as an independent scroll container, and set the scroll area size of the scroll container; Measure the height of each image item, and calculate the visible area of ​​the image at the current scroll position based on the visible area of ​​the scroll container and the size of the image item; Only render the currently visible images, and recycle the images that have been scrolled out of the viewport and are no longer visible; Set a placeholder for each image item, which is a low-resolution loading indicator; Use React's listening function to monitor the distance of each image item from the top of the viewport; When the image enters the visible area, start loading the corresponding high-definition image. When the high-definition image is loaded, replace the corresponding placeholder with it. If the image fails to load, show an error message and fall back to the default image.

6. The method according to claim 5, characterized in that The basic style of the image is defined in the ImageItem component, the hover style is dynamically applied through the hovered state, and the CSS transition property is used to define the transition effect of zooming in and out of the image, including: Define the basic image style class and the hover style class. The basic image style class is used to describe the basic image style, and the hover style class is used to describe the hover style. The basic image style class contains the basic image style settings, including width, height, and border; The hover style class contains the style settings for images when they are hovered. The style settings for images when they are hovered include setting the magnification effect and transition effect. The magnification effect uses the scale() function in the transform attribute of CSS to enlarge the image. The transition effect uses the transition attribute of CSS to define the transition effect, including the transition properties, transition duration, and transition easing function.

7. The method according to claim 6, characterized in that The onMouseEnter and onMouseLeave event processing functions are used to update the hovered state, and the style attributes in styled-components are used to apply different styles according to the hovered state. When it is determined that the current image is in the hovered state, the image is enlarged and the width of the image container is adjusted, including: Use the styled-components library to create two styled components: ImageContainer and StyledImage. In the style of ImageContainer, the width of the container is dynamically set according to the hovered state. When hovered is true, the first width value is set to display the enlarged image. When hovered is false, it is set to the default width. In the StyledImage style, dynamically adjust the image magnification ratio according to the hovered state, and use the transform: scale() property of CSS to achieve the image magnification effect. When hovered is true, set a zoom ratio greater than 1. When hovered is false, keep the original ratio or set it to 1. Add onMouseEnter and onMouseLeave event handling functions to the ImageContainer component; When the mouse enters the image container, the first update function is called to update the hovered state to true, thereby triggering a style change, enlarging the image and adjusting the container width; When the mouse leaves the image container, the second update function is called to update the hovered state to false, restoring the image and container to their original state; In the rendering part of the React component, use ImageContainer to wrap StyledImage, pass the source path src of the image as a property to StyledImage, and ensure that the hovered state is passed as a property to the image container ImageContainer and the image style StyledImage, so that the image container ImageContainer and the image style StyledImage dynamically adjust the style according to the hovered state.

8. A device for forming a picture magnification special effect based on React mouse hover, characterized in that: include: The first creation module creates an ImageItem component, which receives a src attribute for rendering an image, and uses a useState hook in the ImageItem component to manage a Boolean state hovered, which is used to indicate whether the current image is in a hovering state; The second creation module creates an Accordion component, which contains multiple ImageItem subcomponents. The ImageItem components are arranged horizontally using CSS Flexbox layout. An array of image paths is defined in the Accordion component, and the map method of the array is used to dynamically render multiple ImageItem components. Define the module, define the basic style of the image in the ImageItem component, dynamically apply the hover style through the hovered state, and use the CSS transition property to define the transition effect of zooming in and out of the image; In the zoom module, in the ImageItem component, the onMouseEnter and onMouseLeave event processing functions are used to update the hovered state. The style attributes in styled-components are used to apply different styles according to the hovered state. When it is determined that the current image is in the hovered state, the image is zoomed in and the width of the image container is adjusted to achieve the image zooming effect. The creation of the Accordion component includes: Define an array of image paths inside the Accordion component, use the map method to traverse the array of image paths, create an ImageItem component instance for each path, use CSS Flexbox layout to arrange the ImageItem components horizontally, and add media queries in CSS to adapt to different screen sizes; Use Flexbox layout to arrange ImageItem components horizontally and set preset spacing. In responsive design, use media queries to change the layout. The Accordion component obtains the state of the ImageItem through React's callback function Context API. In the ImageItem component, the callback function is passed using props. Set the image processing function in the Accordion component. When the number of images exceeds the preset value, the performance of the images will be optimized.

9. An electronic device, characterized in that: The electronic device comprises: at least one processor; and, a memory communicatively connected to the at least one processor; wherein, The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the method for forming a picture magnification special effect based on React mouse hovering as described in any one of claims 1 to 7.

Citation Information

Patent Citations

  • React-based icon generation method, system and equipment and medium

    CN117421509A

  • Image processing method and device based on React framework, equipment and storage medium

    CN117950559A

  • Method for dynamically displaying multimedia contents

    WO2017134487A1