synchronized hover of two different elements

pual

I'm trying to make a website with a duplicated body in a div called #front. There is also the classic body which is named #back.

What I would like to do is to have synchronized hovers over the two differents divs #front & #back. So this is working fine when there is only one, but it's not as soon as you add more links, since it will apply the effects to all the a and not only the one you are hovering and it's corresponding one in the other div.

For exemple: I would like to hover "amet" in #back to have "amet in #front in the same style but I don't want "other amet" in #back and #front with the same style.

I was wondering if this could be done without having ids on the elements, but if it's not, it's also great. Thanks for the help!

$("a").on({
    mouseenter: function () {
     
    $("#front a, #back a").css({
    "font-style": "italic"
    })
    },
    mouseleave: function () {
   
    $("#front a, #back a").css({
    "font-style": "normal"
    })
    }
});
.scroll {
  position: absolute;
  display: block;
  top: 0;
  height: 100%;
 
}
#front {
  overflow: hidden;
  position: absolute;
  background-color: grey;
  color: white;
  right: 0;
  left: 0;
  bottom: 0;
  top: 0;
  margin: auto auto auto auto;
  width: 25%;
  height: 35%;
  font-size: 2rem;
}
#back {
  overflow: auto;
  font-size: 2rem;
}

p {
  margin: 0;
  padding: 0;
}

a:hover{
  font-style: italic;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class ="scroll" id="back">
  <a href="#">amet</a> &
  <a href="#">other amet</a>
</div>

<div class ="scroll" id="front">
  <a href="#">amet</a> &
  <a href="#">other amet</a>
</div>

Rory McCrossan

To do what you require you can relate the two elements by index within their respective containers using :nth-child. Try this:

$("a").on('mouseenter mouseleave', e => {
  let index = $(e.target).index();
  $(`#front a:nth-child(${index + 1}), #back a:nth-child(${index + 1})`).css('font-style', e.type == 'mouseenter' ? 'italic' : 'normal')
});
.scroll {
  position: absolute;
  display: block;
  top: 0;
  height: 100%;
}

#front {
  overflow: hidden;
  position: absolute;
  background-color: grey;
  color: white;
  right: 0;
  left: 0;
  bottom: 0;
  top: 0;
  margin: auto auto auto auto;
  width: 25%;
  height: 35%;
  font-size: 2rem;
}

#back {
  overflow: auto;
  font-size: 2rem;
}

p {
  margin: 0;
  padding: 0;
}

a:hover {
  font-style: italic;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="scroll" id="back">
  <a href="#">amet</a> &amp;
  <a href="#">other amet</a>
</div>

<div class="scroll" id="front">
  <a href="#">amet</a> &amp;
  <a href="#">other amet</a>
</div>

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

mean of non zero elements - why are these two attempt returning different results?

분류에서Dev

Every two elements mean between these two element

분류에서Dev

:hover over <div> does not apply to all its child elements

분류에서Dev

Triggering audio on hover and triggering different audio on click of same div?

분류에서Dev

Add two values from two different Lists

분류에서Dev

Gap Between two two dates of different cells

분류에서Dev

How to install two different compilers?

분류에서Dev

combine two different queries into one

분류에서Dev

Make :hover on Android require two clicks to open a link

분류에서Dev

ActionScript 2.0 - Button hover visible false two other buttons

분류에서Dev

Masonry layout using different sized circular elements

분류에서Dev

XSD multiple elements with same name but different types

분류에서Dev

Summing elements of different range for each row and replace

분류에서Dev

Putting inputs dynamically, avoid repeating on different elements

분류에서Dev

Synchronized Neighbours

분류에서Dev

How to compare two object elements in a mongodb array

분류에서Dev

Adding two elements side by side, responsive

분류에서Dev

matching and extracting elements with two data frames

분류에서Dev

How to compare elements of two input lists?

분류에서Dev

Keeping two elements inline on responsive display

분류에서Dev

Grouping elements by two fields on a space delimited file

분류에서Dev

Algorithm for comparisons of elements in a two dimensional array

분류에서Dev

Compare two arrays and print number of matching elements

분류에서Dev

Joining two lists with unequal number of elements into a dictionary

분류에서Dev

Two Association Label Elements in Simple Form

분류에서Dev

Same ip in two different hosts but with different netmask, will work?

분류에서Dev

How to compare two different values of different functions or same function?

분류에서Dev

MongoDB merge two aggregations from different collections

분류에서Dev

How to combine the results of two different queries with mongoose?

Related 관련 기사

  1. 1

    mean of non zero elements - why are these two attempt returning different results?

  2. 2

    Every two elements mean between these two element

  3. 3

    :hover over <div> does not apply to all its child elements

  4. 4

    Triggering audio on hover and triggering different audio on click of same div?

  5. 5

    Add two values from two different Lists

  6. 6

    Gap Between two two dates of different cells

  7. 7

    How to install two different compilers?

  8. 8

    combine two different queries into one

  9. 9

    Make :hover on Android require two clicks to open a link

  10. 10

    ActionScript 2.0 - Button hover visible false two other buttons

  11. 11

    Masonry layout using different sized circular elements

  12. 12

    XSD multiple elements with same name but different types

  13. 13

    Summing elements of different range for each row and replace

  14. 14

    Putting inputs dynamically, avoid repeating on different elements

  15. 15

    Synchronized Neighbours

  16. 16

    How to compare two object elements in a mongodb array

  17. 17

    Adding two elements side by side, responsive

  18. 18

    matching and extracting elements with two data frames

  19. 19

    How to compare elements of two input lists?

  20. 20

    Keeping two elements inline on responsive display

  21. 21

    Grouping elements by two fields on a space delimited file

  22. 22

    Algorithm for comparisons of elements in a two dimensional array

  23. 23

    Compare two arrays and print number of matching elements

  24. 24

    Joining two lists with unequal number of elements into a dictionary

  25. 25

    Two Association Label Elements in Simple Form

  26. 26

    Same ip in two different hosts but with different netmask, will work?

  27. 27

    How to compare two different values of different functions or same function?

  28. 28

    MongoDB merge two aggregations from different collections

  29. 29

    How to combine the results of two different queries with mongoose?

뜨겁다태그

보관