如何使用 . hide () 、.show () 或.toggle () 来切换元素的可见度 ?
如果元素可见或隐藏, 我如何测试 ?
如何使用 . hide () 、.show () 或.toggle () 来切换元素的可见度 ?
如果元素可见或隐藏, 我如何测试 ?
当前回答
元素能见度和支流如何运作;
可用下列显示方式隐藏元素: 无、 可见度: 隐藏或不透明: 0。 这些方法之间的差异 :
显示: 没有隐藏元素, 它不占用任何空间; 可见性: 隐藏元素, 但是它仍然在布局中占据空间; 透明性: 0 将元素隐藏为“ 可见性: 隐藏 ” , 它仍然在布局中占据空间; 唯一不同之处在于不透明让元素部分透明; 如果 $ ('. target') , 是 (': hidden' ) {( $ ('. target'. show (); ) 其它 { ($ ('. target'. hide ()); } 如果 ($ ('. target') ) { $ ('. target'). hide (); 其它 { ( $ (' 目标) 。
其他回答
$( "div:visible" ).click(function() {
$( this ).css( "background", "yellow" );
});
$( "button" ).click(function() {
$( "div:hidden" ).show( "fast" );
});
api 文档: 可见选择器
如果隐藏在类 - d - no 类中
if (!$('#ele').hasClass('d-none')) {
$('#ele').addClass('d-none'); //hide
}
hideShow(){
$("#accordionZiarat").hide();
// Checks CSS content for display:[none|block], ignores visibility:[true|false]
if ($("#accordionZiarat").is(":visible")) {
$("#accordionZiarat").hide();
}
else if ($("#accordionZiarat").is(":hidden")) {
$("#accordionZiarat").show();
}
else{
}
用于检查元素是否可见、不显示,甚至不透明度水平的扩展功能
如果元素不可见, 它返回错误 。
function checkVisible(e) {
if (!(e instanceof Element)) throw Error('not an Element');
const elementStyle = getComputedStyle(e);
if (elementStyle.display === 'none' || elementStyle.visibility !== 'visible' || elementStyle.opacity < 0.1) return false;
if (e.offsetWidth + e.offsetHeight + e.getBoundingClientRect().height +
e.getBoundingClientRect().width === 0) {
return false;
}
const elemCenter = {
x: e.getBoundingClientRect().left + e.offsetWidth / 2,
y: e.getBoundingClientRect().top + e.offsetHeight / 2
};
if (elemCenter.x < 0 || elemCenter.y < 0) return false;
if (elemCenter.x > (document.documentElement.clientWidth || window.innerWidth)) return false;
if (elemCenter.y > (document.documentElement.clientHeight || window.innerHeight)) return false;
let pointContainer = document.elementFromPoint(elemCenter.x, elemCenter.y);
do {
if (pointContainer === e) return true;
} while (pointContainer = pointContainer.parentNode);
return false;
}
if($(element).is(":visible")) {
console.log('element is visible');
} else {
console.log('element is not visible');
}