[英]how can I keep the space allocated in DOM when using CSS display:none
I am aware of display:none and visibility:hidden functionality. 我知道display:none和visible:hidden功能。 Due to some reason I have to use display:none and still want to keep the space allocated for the element in DOM so that it doesn't affect other elements next to it. 由于某些原因,我不得不使用display:none,并且仍然想保留为DOM中的元素分配的空间,以免影响它旁边的其他元素。 Is it possible? 可能吗?
This is a follow up question as I have pointed out the issue is because of display:none property. 正如我所指出的,这是一个后续问题,原因在于display:none属性。 Link to the original question is HERE 原始问题的链接在这里
UPDATE: As requested by TJ Crowder, I am including all the relevant details from my original question over here. 更新:根据TJ Crowder的要求,我将原始问题的所有相关细节都包括在这里。
Can you tell me how can I add a nice fade effect for smoother animation to my function instead of setting visibility hidden / visible at an regular interval. 您能告诉我如何为功能添加更漂亮的淡入淡出效果以使动画更流畅,而不是将可见性设置为隐藏/可见吗?
I am not looking for a plugin or add jQuery UI library. 我不是在寻找插件或添加jQuery UI库。
My JS : 我的JS :
setBlinkingInterval: function(elem, event) {
if (intervalIdForBlinking != 0)
window.clearInterval(intervalIdForBlinking);
$(elem).show();
intervalIdForBlinking = setInterval(function() {
if (eventsObj.eventIsFinished(event)) {
timer.setClosedStatus(elem, event);
}
else {
if (elem.css('visibility') == 'hidden')
elem.css('visibility', 'visible');
else
elem.css('visibility', 'hidden');
}
}, 500);
}
Update 1: HTML markup in order to clarify one answer 更新1:HTML标记以阐明一个答案
$('<span/>')
.append('<div id="closing_blink" class="yellowText" style="display:none;">' + closing + ' </div>')
.append(date.formatFullDate(new Date(event.timeUtc)) + timezone)
.append('<br/>')
.append((weatherInfo != '' && trackInfo != '') ? '<div class="whiteText">' + weather + '</div>' + '<div class="orangeText">' + weatherInfo + '</div>' + ' ' + '<div class="whiteText">' + track + '</div>' + '<div class="orangeText">' + trackInfo + '</div>' : '')
.appendTo(rightTd);
So after implementing the solutions based on the provided answers I am having issues when it is displayed on the page. 因此,在根据提供的答案实施解决方案之后,当页面上显示该解决方案时,我遇到了问题。
Case 1: When using my original solution (It works fine) 情况1:使用我的原始解决方案时(工作正常)
Screen recorder link HERE 屏幕录像机链接在这里
Case 2: When using fade in/out method (Display issue) 情况2:使用淡入/淡出方法(显示问题)
Screen recorder link HERE 屏幕录像机链接在这里
Case 3: When using toggle method (Display issue) 情况3:使用切换方法时(显示问题)
Screen recorder link HERE 屏幕录像机链接在这里
Is there any quick fix to solve the display issue? 是否有解决显示问题的快速解决方案?
Here is the complete HTML up generated by a JS function drawRaceHead: function(event) { 这是JS函数drawRaceHead生成的完整HTML内容:function(event){
// Returning all race numbers to default values
styling.makeAllRaceNumbersUnselected();
// Make the race number active (including Racing Specials)
styling.makeCurrentEventNumberSelected(event)
// Race info
$("#raceInfo").html('');
$("#raceInfo").append($('<table/>').append($('<tr/>')))
var leftTd = $('<td style="width: 295px"/>')
.appendTo($('#raceInfo')),
rightTd = $('<td/>')
.appendTo($('#raceInfo'));
// If not Racing Specials category
if (event.parentCategoryId != 2863) leftTd.html(raceFullName + ' ' + event.name)
else leftTd.html(event.name);
$('<div id="closing_time" style="display:none"/>')
.appendTo(leftTd)
// Date, time, weather, track
var weatherInfo = '', trackInfo = '';
if (event.markets.length > 0) {
weatherInfo = (event.markets[0].weather == null) ? '-' : event.markets[0].weather;
trackInfo = (event.markets[0].track == null) ? '-' : event.markets[0].track;
}
var isMSIE = /*@cc_on!@*/false;
var ieVersion = (function(reg) { return isMSIE && navigator.userAgent.match(reg) ? RegExp.$1 * 1 : null; })(/MSIE\s([0-9]+[\.0-9]*)/);
if (isMSIE && ieVersion < 11) {
timezone = '';
}
else {
var regExp = /\(([^)]+)\)/, timezone = (regExp.exec(new Date)[1]).split(' ')[0];
timezone = ' (' + timezone + ')';
}
$('<span/>')
.append('<div id="closing_blink" class="yellowText" style="display:none;">' + closing + ' </div>')
.append(date.formatFullDate(new Date(event.timeUtc)) + timezone)
.append('<br/>')
.append((weatherInfo != '' && trackInfo != '') ? '<div class="whiteText">' + weather + '</div>' + '<div class="orangeText">' + weatherInfo + '</div>' + ' ' + '<div class="whiteText">' + track + '</div>' + '<div class="orangeText">' + trackInfo + '</div>' : '')
.appendTo(rightTd);
}, },
Note: This answer has sections, based on updates from the OP. 注意:此答案包含部分内容,基于OP的更新。
Instead of display: none
, use visibility: hidden
. 代替display: none
,使用visibility: hidden
。 That hides the element, but the element still takes up space in the layout. 这隐藏了元素,但是元素仍然占用布局中的空间。
You can see the difference by comparing this jsbin using display: none
with this one using visibility: hidden
. 你可以看到通过比较差这jsbin使用display: none
与此一个使用visibility: hidden
。
Both of those use this HTML: 两者都使用以下HTML:
<div>This is above</div>
<div id="toggleme">This is the one that toggles</div>
<div>This is below</div>
The first uses: 第一次使用:
(function() {
"use strict";
var toggleme = document.getElementById("toggleme");
setInterval(function() {
if (toggleme.style.visibility === "hidden") {
toggleme.style.visibility = "";
}
else {
toggleme.style.visibility = "hidden";
}
}, 400);
});
while the second uses: 而第二个使用:
(function() {
"use strict";
var toggleme = document.getElementById("toggleme");
setInterval(function() {
if (toggleme.style.display === "none") {
toggleme.style.display = "block";
}
else {
toggleme.style.display = "none";
}
}, 400);
});
You've said in a comment below you're using fadeOut
to hide the element. 您在下面的评论中说过,您正在使用fadeOut
隐藏元素。 That will, of course, set display: none
when done. 当然,这将设置display: none
完成后display: none
。 As Arun points out, you can use fadeTo
instead, which animates opacity
down to 0
, which has a similar effect to setting visibility: hidden
: Live Copy 正如Arun指出的那样,您可以改用fadeTo
来将opacity
降低到0
,这与设置visibility: hidden
具有相似的效果visibility: hidden
: Live Copy
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<meta charset=utf-8 />
<title>fadeTo</title>
</head>
<body>
<div>This is above</div>
<div id="hideme">This is the one that toggles</div>
<div>This is below</div>
<script>
$("#hideme").fadeTo("slow", 0);
</script>
</body>
</html>
Another option is to use the "complete" callback on fadeOut
to change display
and visibility
: Live Copy 另一种选择是在fadeOut
上使用“完成”回调来更改display
和visibility
: 实时复制
$("#hideme").fadeOut(function() {
$(this).css({
display: "",
visibility: "hidden"
});
});
You've asked how you can apply the above to your code. 您已经问过如何将以上内容应用于代码。 The code looks overly-complex to me (not sure what the whole eventObj
thing is about), but here's something I think you can readily adapt: Live Copy 该代码对我来说看起来过于复杂(不确定整个eventObj
的内容是什么),但是我认为您可以轻松适应以下情况: Live Copy
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<meta charset=utf-8 />
<title>faded blinking</title>
</head>
<body>
<div>This is above</div>
<div id="toggleme">This is the one that toggles</div>
<div>This is below</div>
<input type="button" id="btnStartStop" value="Start">
<script>
(function() {
var obj = {
startBlinking: function($elem) {
if ($elem.data("blinking")) {
return;
}
$elem.data("blinking", true);
fadeToZero();
function fadeToZero() {
if ($elem.data("blinking")) {
$elem.fadeTo("slow", 0, fadeToFull);
}
}
function fadeToFull() {
if ($elem.data("blinking")) {
$elem.fadeTo("slow", 1, fadeToZero);
}
}
},
stopBlinking: function($elem) {
$elem.data("blinking", false);
}
};
$("#btnStartStop").click(function() {
if (this.value === "Start") {
obj.startBlinking($("#toggleme"));
this.value = "Stop";
}
else {
obj.stopBlinking($("#toggleme"));
this.value = "Start";
}
})
})();
</script>
</body>
</html>
Use css: 使用CSS:
.hidemeplease {
visibility:hidden;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.