如何在悬停按钮时通过文本切换图像,并延迟反向切换
How to toggle an image by text, on hover of a button, and delay the reverse toggle
我有下面的html,其中有一个由图像表示的互动程序,它下面有一个小的"whatsthis"图标。当人悬停在图标上时,我想显示一些文本来代替图像,当人悬停时隐藏它。
<div id="tileImage" class="tile1 tileDiv"></div>
<div id="tileText" class="tileDiv" style="display:none">
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas ac eros vitae nulla tincidunt volutpat. Etiam tortor ante, consequat in laoreet id, bibendum eget libero. Suspendisse vel arcu purus, id laoreet massa. </span>
</div>
<div id="tileContent">
<span>Something</span>
<div id="whatsthis"></div>
</div>
我已经编写了以下javascript
$(document).ready(function(){
$("#whatsthis").hover(function(){
$(this).parent().parent().find("#tileImage").hide();
$(this).parent().parent().find("#tileText").show();
},function(){
$(this).parent().parent().find("#tileText").delay(1000).hide();
$(this).parent().parent().find("#tileImage").show();
});
});
它运行良好,但有两件事
- 我不希望文本在悬停时立即消失,那么如何添加延迟
- 如果光标从"whatthis"图标移动到描述文本,那么我不希望发生切换效果
我该怎么做?
阅读JQuery文档:
只有队列中的后续事件才会延迟例如,这不会延迟不使用效果队列的.show()或.hide()的无参数形式。
我创建了一个小解决方案。。。不是最好的,但看看吧。
$(document).ready(function(){
var hideShow = function( el ){
var titleImageEl;
var titleTextEl;
function init(){ ativateListener(); }
function ativateListener(){
$(el).mouseenter(function(){
titleImageEl = $(this).parent().parent().find("#tileImage");
titleTextEl = $(this).parent().parent().find("#tileText");
titleImageEl.hide();
titleTextEl.show();
});
$(el).mouseleave({root: this}, delay);
}
function deactivateListener(){
$(el).unbind('mouseenter');
$(el).unbind('mouseleave');
}
function exit(){
titleImageEl.show();
titleTextEl.hide();
ativateListener();
}
function delay(){
deactivateListener();
setTimeout(exit, 1000);
}
return {
init: init
}
};
$('body #whatsthis').each(function(i) {
new hideShow($(this)).init();
});
});
使用onmouseover和onmouseout可能会更好,但这里是修复的jQuery
$(document).ready(function(){
$("#whatsthis").hover(function(){
$("#tileImage").hide();
$("#tileText").show();
},function(){
setTimeout( function() {
$("#tileText").hide();
$("#tileImage").show();
}, 1000 );
});
});
相关文章:
- 延迟加载背景图像时防止双重请求
- 使用JavaScript延迟图像加载
- 在引用磁盘上的图像时,Cordova中的短延迟(500ms?)
- 实现延迟在滚动上加载未知大小的图像并最佳定位的图像库
- Javascript (Mobile) - 使用 jquery 延迟加载图像
- 在定时器/延迟的画布上移动不同的图像
- Javascript - 预加载的图像渲染延迟(仅限第一次)
- 延迟加载带有图像的HTML5部分
- 延迟加载由 appendChild() 在 javascript/leaflet 中附加的图像
- 在附加内容之前延迟加载图像
- 将图像不可见地预加载到 DOM 中,而不会延迟页面加载
- 在延迟加载的图像上使用jQuery缩放
- 延迟一些jQuery函数,直到所有图像完全加载
- jQuery 在实时搜索后延迟加载图像
- 在大多数移动浏览器上,图像仅在出现重大延迟后才会更改
- jQuery幻灯片放映图像延迟
- 图像延迟后褪色
- 图像延迟加载响应式布局
- jQuery图像延迟加载仍然是优化网页加载速度的解决方案
- 用于ftscroll的图像延迟加载器(AngularJS)