简体   繁体   English

jQuery-悬停功能覆盖点击功能

[英]jQuery - hover function is overriding click function

I have a very simple objective that I'm trying to accomplish with the use of jQuery. 我有一个非常简单的目标,试图通过使用jQuery来实现。 I have 6 "trigger" divs that add "selected" classes to corresponding paragraphs. 我有6个“触发器” div,它们将“选定的”类添加到相应的段落中。 It's currently set up and working so that when you hover over div 1, paragraph 1 gets a class toggled. 它当前已设置并正在运行,因此,当您将鼠标悬停在div 1上时, paragraph 1 paragraph会切换一个类。 I want to take this further so that if you actually CLICK div 1, the "selected" class persists on the paragraph until you either click another trigger div or click elsewhere on the document. 我想进一步说明这一点,以便如果您实际上单击了div 1,则“ selected”类将保留在该段落上,直到您单击另一个触发器div或在文档的其他位置单击为止。

Here's a Codepen showing what I have now: http://codepen.io/trevanhetzel/pen/yKnAf 这是一个显示我现在拥有的Codepen: http ://codepen.io/trevanhetzel/pen/yKnAf

And here's the code: 这是代码:

<div class="triggers">
  <div class="one"></div>
  <div class="two"></div>
  <div class="three"></div>
  <div class="four"></div>
  <div class="five"></div>
  <div class="six"></div>
</div>

<div class="paragraphs">
  <p class="one"></p>
  <p class="two"></p>
  <p class="three"></p>
  <p class="four"></p>
  <p class="five"></p>
  <p class="six"></p>
</div>

// The goal is to toggle the selected class when you hover over a trigger, BUT if you click on a trigger it will not just toggle the class, but add it so it's "stuck" until you either click out of the .paragraphs containing div or click on another trigger. Right now, I think the hover functions are overriding the click functions....also I need to minimize all these stupid functions to keep it DRY...

// Toggle selected class when hovering the triggers
$('.triggers .one').hover(function() {
  $('.paragraphs p.one').toggleClass('selected');
});

$('.triggers .two').hover(function() {
  $('.paragraphs p.two').toggleClass('selected');
});

$('.triggers .three').hover(function() {
  $('.paragraphs p.three').toggleClass('selected');
});

$('.triggers .four').hover(function() {
  $('.paragraphs p.four').toggleClass('selected');
});

$('.triggers .five').hover(function() {
  $('.paragraphs p.five').toggleClass('selected');
});

$('.triggers .six').hover(function() {
  $('.paragraphs p.six').toggleClass('selected');
});

// Add the selected class when clicking a trigger
$('.triggers .one').click(function() {
  $('.paragaphs p').removeClass('selected');
  $('.paragraphs p.one').addClass('selected');
});

$('.triggers .two').click(function() {
  $('.paragaphs p').removeClass('selected');
  $('.paragraphs p.two').addClass('selected');
});

$('.triggers .three').click(function() {
  $('.paragaphs p').removeClass('selected');
  $('.paragraphs p.three').addClass('selected');
});

$('.triggers .four').click(function() {
  $('.paragaphs p').removeClass('selected');
  $('.paragraphs p.four').addClass('selected');
});

$('.triggers .five').click(function() {
  $('.paragaphs p').removeClass('selected');
  $('.paragraphs p.five').addClass('selected');
});

$('.triggers .six').click(function() {
  $('.paragaphs p').removeClass('selected');
  $('.paragraphs p.six').addClass('selected');
});

An example. 一个例子。 Try to keep index with 'data-index', and add/remove a class for 'selected'. 尝试使索引与“数据索引”保持一致,并添加/删除“选定”类。

http://codepen.io/anon/pen/lzpCf http://codepen.io/anon/pen/lzpCf

$('.triggers div')
.hover(function () {    // on enter...
        var thisIndex = $(this).data('index')
        isClicked = $(this).hasClass('clicked');

        if (!isClicked) {
            $('.paragraphs p[data-index=' + thisIndex + ']').addClass('selected');
        }
    },
    function () {    // on leave...
        var thisIndex = $(this).data('index');
        isClicked = $(this).hasClass('clicked');

        if (!isClicked) {
            $('.paragraphs p[data-index=' + thisIndex + ']').removeClass('selected');
        }
    })
.click(function () {    // on click...
    $(this).toggleClass('clicked');
});

You can maybe clean-it up more and make one function to handle 'enter' and 'leave'. 您也许可以清理更多内容,并创建一个函数来处理“输入”和“离开”。 Just gave an example. 只是举一个例子。

You can do this by simply adding another class to paragraph corresponding the clicked div. 您可以通过简单地将另一个类添加到与单击的div对应的段落中来实现。 Also you can simplify binding events as following instead of binding event to each div sepaperately. 同样,您可以按以下方式简化绑定事件,而不是将事件单独绑定到每个div。

$('.triggers div').hover(function() {
  $('.paragraphs p.'+$(this).attr('class')).toggleClass('selected');
});

Check this out: demo 检查一下: 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM