在取消悬停时使元素悬停,添加关闭按钮以便记住cookie?

时间:2015-07-31 14:06:36

标签: javascript jquery html css cookies

我正在尝试制作工具提示,但我需要它在用户不受影响时保持徘徊。

此外,我正在尝试为该工具提示添加一个关闭按钮,该按钮将保持“悬停状态”,当用户点击它时,我想让它记住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>

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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

首先,您需要更改css中的所有行&#34; .tooltip:hover&#34; to&#34; .tooltip.hover&#34;。 接下来,您需要在工具提示中添加<button class="btn-close"></button>(此按钮将关闭此工具提示)

然后你需要添加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;
    }
})();

答案 2 :(得分:0)

您可以通过向span添加.tooltip-data来添加x 然后使用jQuery激活鼠标悬停上的工具提示,并通过单击x span来删除它。

$(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>