【问题标题】:Make element hover when unhovering, add close button so it remembers cookies?悬停时使元素悬停,添加关闭按钮以便记住cookie?
【发布时间】:2015-07-31 22:44:42
【问题描述】:

我正在尝试制作一个工具提示,但我需要它在用户取消悬停时保持悬停。

另外,我正在尝试向该工具提示添加一个关闭按钮,该按钮将保持“悬停状态”,当用户单击它时,我想让它记住 cookie,这样它就不会再次为该 cookie 打开.

我是 javascript 和 jQuery 的新手,几天来我一直在思考这个问题。

这是我的代码:

@charset "utf-8";
/* Tooltip CSS Created By Deni*/

.tooltip-container {
	width: 400px;
	height: 300px;
	background: #DBDBDB;
	border: 1px solid black;
	margin: 10% auto;
}

/* Start the tooltip css */

.tooltip {
	position: relative;
	z-index: 9998;
	cursor: help;
}
.tooltip > span {
	display: none;
}
.tooltip:hover {
	z-index:  9999;
}
.tooltip:hover .tooltip-data {
	display: block;
	width: 150px;
	padding: 5px;
	color: #dadada;
	background-color: #A35FA1;
	font-size: 11px;
	border-radius: 5px;
	text-decoration: none;
	font-family: century gothic;
	position: absolute;
	bottom: 20px;
	left: -10px;
	text-align: center;
}
.tooltip-container > a {
	margin-right: 10px;
}
.permhover {
	display: block;
	opacity: 1;
}
**HTML:**
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Tooltip</title>
<link href="tooltip.css" rel="stylesheet" type="text/css" />
<script src="jquery-1.11.3.min.js" type="text/javascript"></script>

</head>

<body>
	<div class="tooltip-container">
    	<a href="javascript:;" class="tooltip">This is the information<span class="tooltip-data"> This is one Tooltip!</span></a>   
    	<a href="javascript:;" class="tooltip">This is the information <span class="tooltip-data"> This another one Tooltip!</span></a>   
    </div>

</body>
</html>

我知道这并不多,但感谢所有帮助。非常感谢。

【问题讨论】:

    标签: javascript jquery html css cookies


    【解决方案1】:

    首先,您需要将 css 中的所有行从“.tooltip:hover”更改为“.tooltip.hover”。 接下来您需要将&lt;button class="btn-close"&gt;&lt;/button&gt; 添加到您的工具提示中(此按钮将关闭此工具提示)

    然后你需要添加 jquery 代码,当你悬停它时,它将添加类悬停到工具提示。

    (function(){
        $('.tooltip').each(function(){
            var tooltip = $(this);
            var tooltipId = tooltip.attr('id');
    
            if( !getCookie(tooltipId) ) {
                tooltip.on('mouseenter.tooltip', function(){
                    tooltip.addClass('hover');
    
                    tooltip.find('.btn-close').on('click', function(){
                        var date = new Date();
                        date.setDate(date.getDate() + 1);
    
                        tooltip.removeClass('hover').off('mouseenter.tooltip');
    
                        document.cookie = tooltipId + "=true; path=/; expires=" + date.toUTCString();
                    });
                });
            }
        });
    
        function getCookie(name) {
            var matches = document.cookie.match(new RegExp("(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"));
            return matches ? decodeURIComponent(matches[1]) : undefined;
        }
    })();
    

    【讨论】:

    • 感谢您的回答。我想在用户点击工具提示时让它不再显示,所以我认为最好的方法是使用 cookie。
    • 我编辑了我的变体并添加了 cookies 参数。但是你需要给每个tooltip添加一个ID属性
    • 感谢您的回答,当我尝试此代码时,它不起作用。您能否详细说明此解决方案?
    • 你能用代码筛选你的代码编辑器吗?我会检查它是否有错误
    • 我刚刚检查了代码,一切正常。请确保您为每个工具提示添加了 id-attribute
    【解决方案2】:

    一个可能的解决方案是在链接上注册 onmouseout 事件并显示您的工具提示。

    【讨论】:

      【解决方案3】:

      您可以通过将 span 添加到 .tooltip-data 来添加 x。
      然后使用 jQuery 在 mousehover 上激活工具提示并通过单击 x 跨度将其删除。

      $(function() {
        $('.tooltip').on('mouseover', function() {
          $(this).find('span').show().append('');
        });
        $('.tooltip-data span').on('click', function() {
          $(this).parent().hide();
        });
      });
      @charset "utf-8";
      
      /* Tooltip CSS Created By Deni*/
      
      .tooltip-container {
        width: 400px;
        height: 300px;
        background: #DBDBDB;
        border: 1px solid black;
        margin: 10% auto;
      }
      /* Start the tooltip css */
      
      .tooltip {
        position: relative;
        z-index: 9998;
        cursor: help;
      }
      .tooltip > span {
        display: none;
      }
      .tooltip:hover {
        z-index: 9999;
      }
      .tooltip-data {
        display: block;
        width: 150px;
        padding: 5px;
        color: #dadada;
        background-color: #A35FA1;
        font-size: 11px;
        border-radius: 5px;
        text-decoration: none;
        font-family: century gothic;
        position: absolute;
        bottom: 20px;
        left: -10px;
        text-align: center;
      }
      .tooltip-data span {
        float: right;
      }
      .tooltip-container > a {
        margin-right: 10px;
      }
      .permhover {
        display: block;
        opacity: 1;
      }
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <div class="tooltip-container">
        <a href="javascript:;" class="tooltip">
            This is the information
            <span class="tooltip-data"> This is one Tooltip! <span> x </span></span>
          </a> 
        <a href="javascript:;" class="tooltip">
            This is the information 
            <span class="tooltip-data"> This another one Tooltip!<span> x </span></span>
          </a> 
      </div>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-13
        • 2021-09-18
        • 1970-01-01
        相关资源
        最近更新 更多