You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Elementor Pro文本悬停切换背景图效果修复求助

Fixes for Hover Image Switch with Persistent Text and Revert on Leave

Key Issues in Current Code

  • No mouse leave event handler to revert to the main image
  • Typo in selector: $('all-text') lacks a dot (should be $('.all-text'))
  • Conflicting logic for showing/hiding text elements

Corrected jQuery Code

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
var $ = jQuery;
$(document).ready(function(){
    // Cache elements for better performance
    const $mainImg = $('.img-1');
    const $allImgs = $('.all-img');
    const $allTexts = $('.all-text');

    $('[data-showme]').hover(
        // Mouse enter: Switch to target image, keep text visible
        function(event){
            event.preventDefault();
            const targetClass = $(this).attr('data-showme');
            
            $allImgs.hide();
            $('.' + targetClass).show();
            $allTexts.show();
        },
        // Mouse leave: Revert to main image, keep text visible
        function(){
            $allImgs.hide();
            $mainImg.show();
            $allTexts.show();
        }
    );
});
</script>

Corrected CSS Code

/* Default state: Hide all images except main */
.all-img {
   display: none;
}
.img-1 {
    display: block;
}

/* Ensure text elements are visible by default */
.all-text {
    display: block;
}

Explanation of Changes

  • Added a mouse leave handler to immediately revert to the main image when the cursor moves away
  • Fixed the selector typo to properly target text elements
  • Simplified logic to guarantee text elements stay visible throughout all interactions
  • Cached frequently used elements to improve performance

内容的提问来源于stack exchange,提问作者Samir Malik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 20:39:27