javascript - 以某种方式禁用/删除悬停对页面加载的影响,启用点击事件

标签 javascript jquery html css

<分区>


关闭 7 年前

除了灯箱,我还有一个特殊的图库 slider (在第一张幻灯片上显示 8 张照片,在第二张幻灯片上显示其余照片)。在每一侧,都有一个箭头(左和右)。它们都有悬停效果(标准:不透明度 0.5。悬停:1;)但是当我们在幻灯片 1 上时,我不希望左箭头具有悬停效果。当我滚动到幻灯片 2 时,我希望左箭头获得悬停效果,而右箭头应该移除悬停效果。

HTML:

       <section id="galleriMain_container">
            <div id="galleriSectionTitle_container">
                <p class="galleriSectionTitle">GALLERI</p>
            </div>
            <div id="galleriMiddle_container">
            <div id="galleriNavArrowLeft_container">
                <svg class="navArrowLeft" version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
	 width="50px" height="50px" viewBox="0 0 451.847 451.847" style="enable-background:new 0 0 451.847 451.847;"
	 xml:space="preserve">
<g>
	<path d="M97.141,225.92c0-8.095,3.091-16.192,9.259-22.366L300.689,9.27c12.359-12.359,32.397-12.359,44.751,0
		c12.354,12.354,12.354,32.388,0,44.748L173.525,225.92l171.903,171.909c12.354,12.354,12.354,32.391,0,44.744
		c-12.354,12.365-32.386,12.365-44.745,0l-194.29-194.281C100.226,242.115,97.141,234.018,97.141,225.92z"/>
</g>
</svg>
            </div>
            <div id="galleriesContainer">...</div>
                                    <div id="galleriNavArrowRight_container">
                <svg class="navArrowRight" version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
	 width="50px" height="50px" viewBox="0 0 451.847 451.847" style="enable-background:new 0 0 451.847 451.847;"
	 xml:space="preserve">
<g>
	<path d="M97.141,225.92c0-8.095,3.091-16.192,9.259-22.366L300.689,9.27c12.359-12.359,32.397-12.359,44.751,0
		c12.354,12.354,12.354,32.388,0,44.748L173.525,225.92l171.903,171.909c12.354,12.354,12.354,32.391,0,44.744
		c-12.354,12.365-32.386,12.365-44.745,0l-194.29-194.281C100.226,242.115,97.141,234.018,97.141,225.92z"/>
</g>
</svg>
            </div>

jQuery:

'use strict';

$(document).ready(function() {
    $('.navArrowRight').click(function (){
        $('#galleri_container').animate({'margin-left':'-105.5%'}, 1000);
    });
    $('.navArrowLeft').click(function (){
        $('#galleri_container').animate({'margin-left':'0'}, 1000);
    }); 
});

CSS:

.navArrowLeft:hover {
    opacity: 1;
    transition: opacity 0.4s ease;
}
.navArrowRight:hover {
    opacity: 1;
    transition: opacity 0.4s ease;
}
.navArrowLeft, .navArrowRight{
    margin-top: 235px;
    cursor: pointer;
    fill: #4E4E4E;
    opacity: 0.5;
    transition: opacity 0.4s ease;
}
.navArrowRight {
    -webkit-transform: scaleX(-1);
    -ms-filter: fliph;
    filter: fliph;
    transform: scaleX(-1);
    float:left;
}

我如何使用 jQuery 做到这一点?

谢谢!

最佳答案

您可以简单地为您的两个箭头切换悬停类。

CSS:

.hover:hover {
  opacity: 1;
  transition: opacity 0.4s ease;
}

JS:

初始页面加载:

$(function(){
  $(".navArrowRight").addClass("hover");
});

页面变化:

$(".navArrowRight").toggleClass("hover");
$(".navArrowLeft").toggleClass("hover");

这样,您的代码应该如下所示:

'use strict';

$(document).ready(function() {
    $(".navArrowRight").addClass("hover");
    $('.navArrowRight').click(function (){
        $('#galleri_container').animate({'margin-left':'-105.5%'}, 1000);
        $(".navArrowRight").toggleClass("hover");
        $(".navArrowLeft").toggleClass("hover");
    });
    $('.navArrowLeft').click(function (){
        $('#galleri_container').animate({'margin-left':'0'}, 1000);
        $(".navArrowRight").toggleClass("hover");
        $(".navArrowLeft").toggleClass("hover");
    }); 
});

更新

这不适用于 SVG,因为 JQuery 有问题。

将其与 SVG 标签一起使用:

'use strict';

$(document).ready(function() {
    $(".navArrowRight").attr("class","navArrowRight hover");
    $('.navArrowRight').click(function (){
        $('#galleri_container').animate({'margin-left':'-105.5%'}, 1000);
        toggle();
    });
    $('.navArrowLeft').click(function (){
        $('#galleri_container').animate({'margin-left':'0'}, 1000);
        toggle();
    }); 
});

function toggle(){
    if($(".navArrowRight").attr("class") == "navArrowRight"){
        $(".navArrowRight").attr("class","navArrowRight hover");
        $(".navArrowLeft").attr("class","navArrowLeft");
    } else {
        $(".navArrowRight").attr("class","navArrowRight");
        $(".navArrowLeft").attr("class","navArrowLeft hover");
    }
}

关于javascript - 以某种方式禁用/删除悬停对页面加载的影响,启用点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31766883/

上一篇:html - 无法让图片和文字在同一行

下一篇:css - @mixin 未在 saas 文件中定义

相关文章:

javascript - XHR 进度事件在上传完成之前不会触发?

HTML 无意义元素,纯粹用于用 ID 包装内容

javascript - HTML Canvas,模糊绘制的多边形

html - 如何创建边界偏移量?

jquery - Fancybox 滚动条显示在 chrome 和 safari 而不是 firefox 中?

jquery - jQuery 中的内容 slider 推子

javascript - 如何在 JavaScript 中查找到已知位置的距离

javascript - Google Maps/Fusion Tables Javascript HTML 不会显示/可视化所有多边形数据(通过色标)

javascript - JsLint 'out of scope'错误

javascript - 如何防止 JQuery 按钮提交我的表单?