javascript - 第二次点击手机后白色图标消失

标签 javascript jquery html toggle

这就是我的代码的工作原理:

  1. 点击图标时,下拉内容会淡入
  2. 再次点击时,下拉内容会淡出

我的代码在桌面上运行良好,但不知道为什么在移动设备上出现问题。第二次点击后图标消失。

  1. 在移动设备上看起来不错 enter image description here

  2. 点击并显示内容 enter image description here

  3. 再次点击关闭,但图标消失 enter image description here

希望大家能给我一些建议。谢谢!

$(document).ready(function(){
    $(".advanced_search a").click(function(){
        $(".overlay_search").fadeToggle(200);
		
		var $this = $(this);
		if ($this.hasClass('advancedsearch_icon_active')) {
			$(".advanced_search a").removeClass('advancedsearch_icon_active').addClass('advancedsearch_icon');
		} else if ($this.hasClass('advancedsearch_icon')) {
			$(".advanced_search a").removeClass('advancedsearch_icon').addClass('advancedsearch_icon_active');
		} else {
			$(".advanced_search a").addClass("advancedsearch_icon");
		}
    });
});
body{
  background-color: #ccc;
}
.advancedsearch_icon{
	background: url('https://image.ibb.co/fxUXFQ/filter.png') no-repeat right 0; width: 20px; height: 20px; padding-left: 20px;
}
.advancedsearch_icon:hover, .advancedsearch_icon_active{
	background: url('https://image.ibb.co/kY4opk/filter_hover.png') no-repeat right 0; width: 20px; height: 20px; padding-left: 20px;
}

.overlay_search {
	display:none;
	position:absolute;
	width:100%;
	background:#eeeeee;
	overflow:hidden;
	z-index:3;
	-webkit-box-shadow: 7px 7px 24px -8px rgba(18,17,12,0.5); -moz-box-shadow: 7px 7px 24px -8px rgba(0,0,0,0.5); box-shadow: 7px 7px 24px -8px rgba(18,17,12,0.5);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="advanced_search">
		<a class="advancedsearch_icon" href="#"></a>
</div>
<div class="overlay_search">
  content is here.
</div>

最佳答案

请记住,在移动设备上没有 :hover 事件。您可以使用 :active 但在 :hover 之后使用它。示例 a:hover, a:active { styles } 。也许这就是导致问题的原因

$(document).ready(function(){
    $(".advanced_search a").click(function(){
        $(".overlay_search").fadeToggle(200);
		
		var $this = $(this);
		if ($this.hasClass('advancedsearch_icon_active')) {
			$(".advanced_search a").removeClass('advancedsearch_icon_active').addClass('advancedsearch_icon');
		} else if ($this.hasClass('advancedsearch_icon')) {
			$(".advanced_search a").removeClass('advancedsearch_icon').addClass('advancedsearch_icon_active');
		} else {
			$(".advanced_search a").addClass("advancedsearch_icon");
		}
    });
});
body{
  background-color: #ccc;
}
.advancedsearch_icon{
	background: url('https://image.ibb.co/fxUXFQ/filter.png') no-repeat right 0; width: 20px; height: 20px; padding-left: 20px;
}
.advancedsearch_icon:hover, .advancedsearch_icon_active,
.advancedsearch_icon:active
{
	background: url('https://image.ibb.co/kY4opk/filter_hover.png') no-repeat right 0; width: 20px; height: 20px; padding-left: 20px;
}

.overlay_search {
	display:none;
	position:absolute;
	width:100%;
	background:#eeeeee;
	overflow:hidden;
	z-index:3;
	-webkit-box-shadow: 7px 7px 24px -8px rgba(18,17,12,0.5); -moz-box-shadow: 7px 7px 24px -8px rgba(0,0,0,0.5); box-shadow: 7px 7px 24px -8px rgba(18,17,12,0.5);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="advanced_search">
		<a class="advancedsearch_icon" href="#"></a>
</div>
<div class="overlay_search">
  content is here.
</div>

关于javascript - 第二次点击手机后白色图标消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45369819/

相关文章:

javascript - 在 react 中映射数组时处理点击

javascript - 为什么我会收到错误 : DISPATCH_REQUEST_ERR when trying to click on a Bing Map?

javascript - 如何在 Angular 中替换 body 类 [ 最佳方法 ]

javascript - jQuery AutoComplete 在第一次使用后显示在元素后面

javascript - 在 Angular js中将字节数组显示为图像

javascript - 自动滚动网站始终以相同的速度

javascript - 隐藏在包含 div 滚动条的下方的 Div 溢出 : scroll set

javascript - Bootstrap 3 中的嵌套 Accordion 列表在选择父项时未取消选择

javascript - 在javascript中生成动态对象数组

jQuery 不适用于 iframe 内容