简体   繁体   English

将鼠标悬停在链接上以影响另一个链接

[英]Hover Over Link to Effect Another Link

What im trying to do is when I hover over a anchor tag link on the same page, it also needs to affect the corresponding link. 我想做的是,当我将鼠标悬停在同一页面上的锚标记链接上时,它也需要影响相应的链接。

It might be possible in CSS but I think JQuery would handle this better. 在CSS中可能是可行的,但我认为JQuery会更好地处理此问题。

Im new to jquery 我是jQuery新手

Heres my code: 这是我的代码:

<script>
$('.js-tooltip').on('click', function () {
  $(this).toggleClass('js-tooltip-active')
})
</script>

Heres my CSS: 这是我的CSS:

.tooltip {
  position: relative;
  display: inline-block;
  height: 18px;
  width: 18px;
  line-height: 26px;
  padding: 0 0;
  border-radius: 15px;
  font-size: 12px;
  font-weight: bold;
  color: #FFF;
  background: #b71a71;
  box-shadow: none;
  white-space: nowrap;
  cursor: pointer;
}
.tooltip:hover {
  background: #b1d12d;
}
.tooltip-wrapper {
  pointer-events: none;
  position: absolute;
  width: 250px;
  margin-left: -125px;
  left: 50%;
  bottom: 100%;
  margin-bottom: 5px;
  text-align: center;
  text-decoration: none;
  opacity: 0;
  transition: opacity 0.5s ease;
}
.js-tooltip-active .tooltip-wrapper,
.tooltip:hover .tooltip-wrapper,
.tooltip-wrapper:hover {
  pointer-events: auto;
  opacity: 1;
}
.tooltip-wrapper:after {
  z-index: 11;
  display: block;
  position: absolute;
  left: 50%;
  margin-left: -7px;
  content: " ";
  width: 0;
  height: 0;
  border-left: 7px solid transparent;
  border-right: 7px solid transparent;
  border-top: 7px solid #333;
}
.tooltip-wrapper:before {
  bottom: -9px;
  display: block;
  position: absolute;
  left: 50%;
  margin-left: -8px;
  content: " ";
  width: 0;
  height: 0;
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  border-top: 8px solid rgba(255,255,255,0.1);
}
.tooltip-text {
  display: inline-block;
  position: relative;
  padding: 6px 9px;
  z-index: 10;
  white-space: normal;
  font-size: 12px;
  font-weight: normal;
  line-height: 18px;
  background: #333;
  background: rgba(0,0,0,0.8);
  color: #fff;
  border-radius: 5px;
  text-shadow: none;
  cursor: default;
  box-shadow: 0 1px rgba(255,255,255,0.1);
}



<div class="mapbox"><img src="#" style="z-index: 101; border: none" width="672" height="744"  usemap="#Map"/>
<a class="tooltip js-tooltip manmap" href="#" style="top: -315px; left: 270px; border: none; "><span class="tooltip-wrapper" style="z-index: 103; border: none; "><span class="tooltip-text" style="z-index: 103; cursor: pointer; border: none;">View</span></span></a>
<a class="tooltip js-tooltip lonmap" href="#" style="top: -150px; left: 365px;"><span class="tooltip-wrapper" style="z-index: 103;"><span class="tooltip-text" style="z-index: 103; cursor: pointer;">View</span></span></a>
</div>

What the code above does is when I hover over the hotspot a small title box appears that the user can click. 上面的代码所做的是,当我将鼠标悬停在热点上时,会出现一个小标题框,用户可以单击它。

<div id="col3" class="right">

<h2>Select a location<img src="#" width="21" height="18" alt="icon" /></h2>

<div class="box">
    <h3>Select</h3>
    <ul id="locationList">
        <li class="a"><a href="#">A</a></li>
        <li><a href="#">B</a></li>
    </ul>
</div>

This is the <li> link list that I would like to connect to the map. 这是我想连接到地图的<li>链接列表。

What I want is to try and replicate the effect of the circle hover but on the links, I don't want to show and hide the circle markers on the map I would just like them to appear when the corresponding link has been hovered over. 我想要的是尝试复制圆圈悬停的效果,但是在链接上,我不想在地图上显示和隐藏圆圈标记,我希望它们在相应的链接悬停在上方时出现。

Also the Map markers change colour from purple to green am I able to replicate that effect hovering over the links in the sidebar. 同样,地图标记可以将颜色从紫色更改为绿色,以便能够将效果悬停在侧栏中的链接上。

So basically when I hover over the circle marker the title tag pops up with the link, that is what I would like the link to do as well so I can hover over link and it will do the same and hovering over the circle and vice-versa. 因此,基本上,当我将鼠标悬停在圆圈标记上时,标题标记也会随链接一起弹出,这也是我希望链接执行的操作,因此我可以将鼠标悬停在链接上,它也会做同样的事情并将鼠标悬停在圆圈上,反之,反之亦然。

I don't know if this helps but this is where I got the code for the tooltip/hotspot Heres the link, then I changed the code for it to look circle. 我不知道这是否有帮助,但这是我在此处获得工具提示/热点的代码的地方,这里是链接,然后我更改了代码以使其看起来像圆圈。

Thanks. 谢谢。

Ok....it took a little doing because my Jquery skills are poor so I'm sure this could be refactored and simplified but here goes. 好的....花了点功夫,因为我的Jquery技能很差,所以我确定可以重构和简化它,但是可以了。

We have to add an individual attribute to each list item, I use a data-attribute which can then be used to select each individual map point which will have it's own ID 我们必须为每个列表项添加一个单独的属性,我使用一个data-attribute ,然后可以使用该data-attribute来选择每个具有自己的ID的地图点

JSfiddle Demo JSfiddle演示

Revised HTML 修改后的HTML

<div id="col5" class="left">

    <h1>Pick A Location</h1>


    <div class="mapbox">       

        <a id="A" class="tooltip js-tooltip" href="#">
            <span class="tooltip-wrapper">
                <span class="tooltip-text">View 1</span>
            </span>
        </a>

        <a id="B" class="tooltip js-tooltip" href="#">
            <span class="tooltip-wrapper" >
                <span class="tooltip-text">View 2</span>
            </span>
        </a>        
    </div>
</div>

<div class="box">
    <h3>Select a location</h3>
    <ul id="locationList">
        <li><a data-item="A" href="#">View 1</a></li>
        <li><a data-item="B" href="#">View 2</a></li>
    </ul>
</div>

CSS CSS

I just added an `.active' class for the list item links 我刚刚为列表项链接添加了“ .active”类

#locationList a.active {
    color:red;
}

EDIT- and for the tooltip something similar 编辑-和工具提示类似

.tooltip.current {
  background: #b1d12d;
}

Jquery jQuery的

I added these two functions 我添加了这两个功能

$('.tooltip').hover(function() {
     $('a[data-item="'+this.id+'"]').toggleClass('active');
});

/* when a tooltip is hovered, find the anchor which has a data-item attribute that matches the ID of the hovered element and toggle the active class */ / *悬停工具提示时,找到具有与悬停元素的ID相匹配的data-item属性的锚点,然后切换活动类* /

  $('#locationList a').hover(function() {
    $('#' + $(this).data('item')).toggleClass('js-tooltip-active');
    $('#' + $(this).data('item')).toggleClass('current'); /* EDIT for hover */
});

/* when list item link is hovered, find the matching ID toggle the js-tooltip-active class */ / *悬停列表项链接时,找到匹配的ID切换js-tooltip-active类* /

This is what you need. 这就是你所需要的。 it is no my code all the credit goes to the author of the link below. 这不是我的代码,所有功劳归于以下链接的作者。 Check the link to see the live example. 检查链接以查看实时示例。

    #one:hover ~ #three,
#six:hover ~ #four,
#seven:hover ~ .box {
    background-color: black;
    color: white;
}
#four {
    margin-left: -35px;
}
#six {
    left: 80px;
    position: relative;
}
.box {
    cursor: pointer;
    display: inline-block;
    height: 30px;
    line-height: 30px;
    margin: 5px;
    outline: 1px solid black;
    text-align: center;
    width: 30px;
}

http://jsfiddle.net/ThinkingStiff/a3y52/ http://jsfiddle.net/ThinkingStiff/a3y52/

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

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