如何为这些元素上的 hide() 和 show() 效果添加过渡?
How do I add transition to the effect of hide() and show() on these elements?
在下面的 SSCCE 中,我如何使用 show()
动画化 .item
的出现并使用 hide()
动画化它们的消失,使其看起来像 item5
, item6
, item7
, item8
飞进了视口?
也就是我要的是当.next-arrow
被点击时,比如第一次,隐藏item1
、item2
、item3
, item4
, item5
, item6
, item7
, item8
的显示是 "transitioned" - 就像新页面流入视口一样in this website.
我该怎么做?
$(document).ready(function() {
//alert('ready');//check
var numberOfItems = $('.item').length;
//alert('numberOfItems => ' + numberOfItems);//check
displayNextArrowOnCondition();
displayPreviousArrowOnCondition();
/**
*
**/
$('a.next-arrow').click(function(event) {
event.preventDefault();
var currentFirstItem = getCurrentFirstItem(); // Difference between var and no var SO: If you're in the global scope then there's no difference. If you're in a function then var will create a local variable, "no var" will look up the scope chain until it finds the variable or hits the global scope (at which point it will create it):
$('div.item' + currentFirstItem).hide(); //We don't need to have the condition of checking this element's existence because the next-arrow whose handler this method is, appears only if the numberOfItems is greater than the id of the item with the greatest id among the elements currently visible on the screen.
if (('div.item' + (currentFirstItem + 1)).length) { //SO: How can I check the existence of an element in jQuery?? In JavaScript, everything is truthy or falsy and for numbers, 0 means false, everything else true. So you could write: "if ($(selector).length)" - and you don't need that > 0 part.
$('div.item' + (currentFirstItem + 1)).hide();
}
if (('div.item' + (currentFirstItem + 2)).length) {
$('div.item' + (currentFirstItem + 2)).hide();
}
if (('div.item' + (currentFirstItem + 3)).length) {
$('div.item' + (currentFirstItem + 3)).hide();
}
hidePreviousArrow();
hideNextArrow();
displayPreviousArrowOnCondition();
displayNextArrowOnCondition();
});
/**
*
**/
$('a.previous-arrow').click(function(event) {
event.preventDefault();
var currentFirstItem = getCurrentFirstItem();
$('div.item' + (currentFirstItem - 1)).show();
if (('div.item' + (currentFirstItem - 2)).length) {
$('div.item' + (currentFirstItem - 2)).show();
}
if (('div.item' + (currentFirstItem - 3)).length) {
$('div.item' + (currentFirstItem - 3)).show();
}
if (('div.item' + (currentFirstItem - 4)).length) {
$('div.item' + (currentFirstItem - 4)).show();
}
hidePreviousArrow();
hideNextArrow();
displayPreviousArrowOnCondition();
displayNextArrowOnCondition();
});
/**
* DISPLAY NEXT ARROW WHEN
* 1. NUMBER OF ITEMS IS GREATER THAN THE id OF THE LAST ITEM DISPLAYED IN THE CURRENT VIEWPORT
**/
function displayNextArrowOnCondition() {
//alert('displayNextArrowOnCondition called');//check
//Iterate through items in OPPOSTIE order, and when found the first one which is not hidden by hide() or display:none, assign it to currentLastItem (because this is the first item in the viewport), and break out from the loop.
var currentLastItem = getCurrentLastItem();
//alert('currentLastItem -> ' + currentLastItem);//check
if (currentLastItem < numberOfItems) {
$('a.next-arrow').css('display', 'block');
$('.wrapper').mouseover(function() {
//$('a.next-arrow').css('visibility', 'visible');
});
$('.wrapper').mouseleave(function() {
//$('a.next-arrow').css('visibility', 'hidden');
});
}
}
/**
* DISPLAY PREVIOUS ARROW WHEN
* 1. THE id OF THE FIRST DISPLAYED ITEM IS GREATER THAN 4
**/
function displayPreviousArrowOnCondition() {
//Iterate through items in order, and when found the first one which is not hidden by hide() or display:none, assign it to currentFirstItem (because this is the first item in the viewport), and break out from the loop.
var currentFirstItem = getCurrentFirstItem();
if (currentFirstItem > 4) {
$('a.previous-arrow').css('display', 'block');
$('.wrapper').mouseover(function() {
$('a.previous-arrow').css('visibility', 'visible');
});
$('.wrapper').mouseleave(function() {
$('a.previous-arrow').css('visibility', 'hidden');
});
}
}
/**
* DISPLAY:NONE NEXT ARROW IF IT IS VISIBLE
**/
function hideNextArrow() {
//alert('hideNextArrow called');//check
if ($('a.next-arrow').css('display').toLowerCase() == 'block') { //The == operator will compare for equality after doing any necessary type conversions. The === operator will not do the conversion, so if two values are not the same type === will simply return false.| Just in case anyone was wondering in 2012: === is way faster than ==. jsperf.com/comparison-of-comparisons.
//alert('YES if ($(\'a.next-arrow\').attr(\'display\').toLowerCase() == \'block\'). SO I AM CHANGING IT TO none.');//check
$('a.next-arrow').css('display', 'none');
} //else { alert('NO if ($(\'a.next-arrow\').attr(\'display\').toLowerCase() == \'block\').'); } //check
}
/**
* DISPLAY:NONE PREVIOUS ARROW IF IT IS VISIBLE
**/
function hidePreviousArrow() {
//alert('hidePreviousArrow called');//check
//alert($('a.previous-arrow').css('display'));//check
if ($('a.previous-arrow').css('display') == 'block') {
//alert('YES if ($(\'a.previous-arrow\').attr(\'display\').toLowerCase() == \'block\'). SO I AM CHANGING IT TO none.');//check
$('a.previous-arrow').css('display', 'none');
} //else { alert('NO if ($(\'a.previous-arrow\').attr(\'display\').toLowerCase() == \'block\').'); } //check
}
/**
*
**/
function getCurrentFirstItem() {
for (i = 1; i <= numberOfItems; i++) {
if ($("#" + i).visible(true, true)) {
//alert('YES if ( $("#"+i).visible(true, true) )');//check
currentFirstItem = i;
break;
} //else { //alert('NO if ( $("#"+i).visible(true, true) )'); }//check
}
//alert('currentFirstItem -> ' + currentFirstItem);//check
return currentFirstItem;
}
/**
*
**/
function getCurrentLastItem() {
for (j = numberOfItems; j >= 1; j--) {
if ($("#" + j).visible(true, true)) {
//alert("YES if ( $(\"#\"+j).visible(true, true) ) ");//check
currentLastItem = j;
break;
} //else { //alert('NO if ( $("#"+j).visible(true, true) )'); } //check
}
//alert('currentLastItem -> ' + currentLastItem);//check
return currentLastItem;
}
});
html,
body,
body div,
span,
object,
iframe,
h1,
h2,
h3,
h4,
h5,
h6,
p,
blockquote,
pre,
abbr,
address,
cite,
code,
del,
dfn,
em,
img,
ins,
kbd,
q,
samp,
small,
strong,
sub,
sup,
var,
b,
i,
dl,
dt,
dd,
ol,
ul,
li,
fieldset,
form,
label,
legend,
table,
caption,
tbody,
tfoot,
thead,
tr,
th,
td,
article,
aside,
figure,
footer,
header,
hgroup,
menu,
nav,
section,
time,
mark,
audio,
video,
details,
summary {
margin: 0px;
padding: 0px;
border: 0px none;
background: transparent none repeat scroll 0% 0%;
font-size: 100%;
vertical-align: baseline;
}
.wrapper {
position: relative;
white-space: nowrap;
overflow: hidden;
}
div.item {
/*position:absolute;*/
display: inline-block;
width: 25%;
height: 25vw;
}
.item1 {
left: 0%;
background-color: wheat;
}
.item2 {
left: 25%;
background-color: pink;
}
.item3 {
left: 50%;
background-color: beige;
}
.item4 {
left: 75%;
background-color: gainsboro;
}
.item5 {
left: 100%;
background-color: coral;
}
.item6 {
left: 125%;
background-color: crimson;
}
.item7 {
left: 150%;
background-color: aquamarine;
}
.item8 {
left: 175%;
background-color: darkgoldenrod;
}
.item9 {
left: 200%;
background-color: khaki;
}
.item10 {
left: 225%;
background-color: indianred;
}
.item11 {
left: 250%;
background-color: mediumspringgreen;
}
.previous-arrow,
.next-arrow {
width: 30px;
height: 50%;
top: 50%;
position: absolute;
opacity: 0.7;
color: white;
background-repeat: no-repeat;
margin-top: -30px;
display: none;
}
.previous-arrow {
background-image: url(a2.png);
left: 0px;
}
.next-arrow {
background-image: url(b2.png);
right: 0px;
}
.previous-arrow,
.next-arrow {
opacity: 1;
}
body {
background-color: black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdn.jsdelivr.net/jquery.visible/1.1.0/jquery.visible.min.js"></script>
<div class="wrapper">
<a class="previous-arrow" href="">></a>-->
<!--
-->
<div id="1" class="item item1 wheat">a.</div>
<!--
-->
<div id="2" class="item item2 pink">a.</div>
<!--
-->
<div id="3" class="item item3 beige">a.</div>
<!--
-->
<div id="4" class="item item4 gainsboro">a.</div>
<!--
-->
<div id="5" class="item item5 coral">a.</div>
<!--
-->
<div id="6" class="item item6 crimson">a.</div>
<!--
-->
<div id="7" class="item item7 darkgoldenrod">a.</div>
<!--
-->
<div id="8" class="item item8 aquamarine">a.</div>
<!--
-->
<div id="9" class="item item9 aquamarine">a.</div>
<!--
-->
<div id="10" class="item item10 aquamarine">a.</div>
<!--
-->
<div id="11" class="item item11 aquamarine">a.</div>
<!--
--><a class="next-arrow" href=""><</a>
</div>
您可以使用 css 过渡或 css 动画。动画如:
- 定义 css class 规则并为要设置动画的元素指定 class 名称
.doFadeIn {
-webkit-animation:fadeInSlide forwards ease-in 150ms;
animation:fadeInSlide forwards ease-in 150ms;
}
.doFadeOut {
-webkit-animation:fadeOutSlideDown forwards ease-out 150ms;
animation:fadeOutSlideDown forwards ease-out 150ms;
}
- 定义动画的关键帧
@keyframes fadeInSlide
{
0% { opactity:0; transform: translateX(-100px); }
100% { opactity:1; transform: translateX(0); }
}
将 doFadeIn 分配给元素的 class 名称时,它们将:
淡入 - opacity:0 到 1(0% 可见到 100% 可见)
沿元素原始位置左侧的水平轴 (X) 移动 100px 到它的原始位置。
它将在 150 毫秒的持续时间内执行此操作
<div id="my-widget1" class="my-widget"><div>
<div id="my-widget1" class="my-widget doFadeIn"><div>
如果你只想使用show()和hide(),你可以使用show('slow')和hide('slow')等。除此之外还有很多方法和效果你可以使用。
请按照此 link 了解有多少种方法可以做到这一点。
您可以为 show()
函数传递一个参数,例如 .show('slow')
,它将像 fadeIn()
函数一样执行动画。
检查 documentation 示例
您可以使用 .hide()
、fadeOut()
、show()
、fadeIn()
示例代码
您可以使用.show()
来显示匹配的元素。
用户在输入文本中键入内容时显示列表的示例代码。
$("#search-guide").focusin(function () {
$("#list-user-guides").show();
});
您可以使用.hide()
来隐藏匹配的元素。
当用户单击屏幕上的其他地方时隐藏列表的示例代码。
$("#search-guide").focusin(function () {
$("#list-user-guides").hide();
});
但我个人使用 .fadeIn()
和 fadeOut()
因为这两种方法都带有流畅的动画。
上面的回答可以查看官方文档
在下面的 SSCCE 中,我如何使用 show()
动画化 .item
的出现并使用 hide()
动画化它们的消失,使其看起来像 item5
, item6
, item7
, item8
飞进了视口?
也就是我要的是当.next-arrow
被点击时,比如第一次,隐藏item1
、item2
、item3
, item4
, item5
, item6
, item7
, item8
的显示是 "transitioned" - 就像新页面流入视口一样in this website.
我该怎么做?
$(document).ready(function() {
//alert('ready');//check
var numberOfItems = $('.item').length;
//alert('numberOfItems => ' + numberOfItems);//check
displayNextArrowOnCondition();
displayPreviousArrowOnCondition();
/**
*
**/
$('a.next-arrow').click(function(event) {
event.preventDefault();
var currentFirstItem = getCurrentFirstItem(); // Difference between var and no var SO: If you're in the global scope then there's no difference. If you're in a function then var will create a local variable, "no var" will look up the scope chain until it finds the variable or hits the global scope (at which point it will create it):
$('div.item' + currentFirstItem).hide(); //We don't need to have the condition of checking this element's existence because the next-arrow whose handler this method is, appears only if the numberOfItems is greater than the id of the item with the greatest id among the elements currently visible on the screen.
if (('div.item' + (currentFirstItem + 1)).length) { //SO: How can I check the existence of an element in jQuery?? In JavaScript, everything is truthy or falsy and for numbers, 0 means false, everything else true. So you could write: "if ($(selector).length)" - and you don't need that > 0 part.
$('div.item' + (currentFirstItem + 1)).hide();
}
if (('div.item' + (currentFirstItem + 2)).length) {
$('div.item' + (currentFirstItem + 2)).hide();
}
if (('div.item' + (currentFirstItem + 3)).length) {
$('div.item' + (currentFirstItem + 3)).hide();
}
hidePreviousArrow();
hideNextArrow();
displayPreviousArrowOnCondition();
displayNextArrowOnCondition();
});
/**
*
**/
$('a.previous-arrow').click(function(event) {
event.preventDefault();
var currentFirstItem = getCurrentFirstItem();
$('div.item' + (currentFirstItem - 1)).show();
if (('div.item' + (currentFirstItem - 2)).length) {
$('div.item' + (currentFirstItem - 2)).show();
}
if (('div.item' + (currentFirstItem - 3)).length) {
$('div.item' + (currentFirstItem - 3)).show();
}
if (('div.item' + (currentFirstItem - 4)).length) {
$('div.item' + (currentFirstItem - 4)).show();
}
hidePreviousArrow();
hideNextArrow();
displayPreviousArrowOnCondition();
displayNextArrowOnCondition();
});
/**
* DISPLAY NEXT ARROW WHEN
* 1. NUMBER OF ITEMS IS GREATER THAN THE id OF THE LAST ITEM DISPLAYED IN THE CURRENT VIEWPORT
**/
function displayNextArrowOnCondition() {
//alert('displayNextArrowOnCondition called');//check
//Iterate through items in OPPOSTIE order, and when found the first one which is not hidden by hide() or display:none, assign it to currentLastItem (because this is the first item in the viewport), and break out from the loop.
var currentLastItem = getCurrentLastItem();
//alert('currentLastItem -> ' + currentLastItem);//check
if (currentLastItem < numberOfItems) {
$('a.next-arrow').css('display', 'block');
$('.wrapper').mouseover(function() {
//$('a.next-arrow').css('visibility', 'visible');
});
$('.wrapper').mouseleave(function() {
//$('a.next-arrow').css('visibility', 'hidden');
});
}
}
/**
* DISPLAY PREVIOUS ARROW WHEN
* 1. THE id OF THE FIRST DISPLAYED ITEM IS GREATER THAN 4
**/
function displayPreviousArrowOnCondition() {
//Iterate through items in order, and when found the first one which is not hidden by hide() or display:none, assign it to currentFirstItem (because this is the first item in the viewport), and break out from the loop.
var currentFirstItem = getCurrentFirstItem();
if (currentFirstItem > 4) {
$('a.previous-arrow').css('display', 'block');
$('.wrapper').mouseover(function() {
$('a.previous-arrow').css('visibility', 'visible');
});
$('.wrapper').mouseleave(function() {
$('a.previous-arrow').css('visibility', 'hidden');
});
}
}
/**
* DISPLAY:NONE NEXT ARROW IF IT IS VISIBLE
**/
function hideNextArrow() {
//alert('hideNextArrow called');//check
if ($('a.next-arrow').css('display').toLowerCase() == 'block') { //The == operator will compare for equality after doing any necessary type conversions. The === operator will not do the conversion, so if two values are not the same type === will simply return false.| Just in case anyone was wondering in 2012: === is way faster than ==. jsperf.com/comparison-of-comparisons.
//alert('YES if ($(\'a.next-arrow\').attr(\'display\').toLowerCase() == \'block\'). SO I AM CHANGING IT TO none.');//check
$('a.next-arrow').css('display', 'none');
} //else { alert('NO if ($(\'a.next-arrow\').attr(\'display\').toLowerCase() == \'block\').'); } //check
}
/**
* DISPLAY:NONE PREVIOUS ARROW IF IT IS VISIBLE
**/
function hidePreviousArrow() {
//alert('hidePreviousArrow called');//check
//alert($('a.previous-arrow').css('display'));//check
if ($('a.previous-arrow').css('display') == 'block') {
//alert('YES if ($(\'a.previous-arrow\').attr(\'display\').toLowerCase() == \'block\'). SO I AM CHANGING IT TO none.');//check
$('a.previous-arrow').css('display', 'none');
} //else { alert('NO if ($(\'a.previous-arrow\').attr(\'display\').toLowerCase() == \'block\').'); } //check
}
/**
*
**/
function getCurrentFirstItem() {
for (i = 1; i <= numberOfItems; i++) {
if ($("#" + i).visible(true, true)) {
//alert('YES if ( $("#"+i).visible(true, true) )');//check
currentFirstItem = i;
break;
} //else { //alert('NO if ( $("#"+i).visible(true, true) )'); }//check
}
//alert('currentFirstItem -> ' + currentFirstItem);//check
return currentFirstItem;
}
/**
*
**/
function getCurrentLastItem() {
for (j = numberOfItems; j >= 1; j--) {
if ($("#" + j).visible(true, true)) {
//alert("YES if ( $(\"#\"+j).visible(true, true) ) ");//check
currentLastItem = j;
break;
} //else { //alert('NO if ( $("#"+j).visible(true, true) )'); } //check
}
//alert('currentLastItem -> ' + currentLastItem);//check
return currentLastItem;
}
});
html,
body,
body div,
span,
object,
iframe,
h1,
h2,
h3,
h4,
h5,
h6,
p,
blockquote,
pre,
abbr,
address,
cite,
code,
del,
dfn,
em,
img,
ins,
kbd,
q,
samp,
small,
strong,
sub,
sup,
var,
b,
i,
dl,
dt,
dd,
ol,
ul,
li,
fieldset,
form,
label,
legend,
table,
caption,
tbody,
tfoot,
thead,
tr,
th,
td,
article,
aside,
figure,
footer,
header,
hgroup,
menu,
nav,
section,
time,
mark,
audio,
video,
details,
summary {
margin: 0px;
padding: 0px;
border: 0px none;
background: transparent none repeat scroll 0% 0%;
font-size: 100%;
vertical-align: baseline;
}
.wrapper {
position: relative;
white-space: nowrap;
overflow: hidden;
}
div.item {
/*position:absolute;*/
display: inline-block;
width: 25%;
height: 25vw;
}
.item1 {
left: 0%;
background-color: wheat;
}
.item2 {
left: 25%;
background-color: pink;
}
.item3 {
left: 50%;
background-color: beige;
}
.item4 {
left: 75%;
background-color: gainsboro;
}
.item5 {
left: 100%;
background-color: coral;
}
.item6 {
left: 125%;
background-color: crimson;
}
.item7 {
left: 150%;
background-color: aquamarine;
}
.item8 {
left: 175%;
background-color: darkgoldenrod;
}
.item9 {
left: 200%;
background-color: khaki;
}
.item10 {
left: 225%;
background-color: indianred;
}
.item11 {
left: 250%;
background-color: mediumspringgreen;
}
.previous-arrow,
.next-arrow {
width: 30px;
height: 50%;
top: 50%;
position: absolute;
opacity: 0.7;
color: white;
background-repeat: no-repeat;
margin-top: -30px;
display: none;
}
.previous-arrow {
background-image: url(a2.png);
left: 0px;
}
.next-arrow {
background-image: url(b2.png);
right: 0px;
}
.previous-arrow,
.next-arrow {
opacity: 1;
}
body {
background-color: black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdn.jsdelivr.net/jquery.visible/1.1.0/jquery.visible.min.js"></script>
<div class="wrapper">
<a class="previous-arrow" href="">></a>-->
<!--
-->
<div id="1" class="item item1 wheat">a.</div>
<!--
-->
<div id="2" class="item item2 pink">a.</div>
<!--
-->
<div id="3" class="item item3 beige">a.</div>
<!--
-->
<div id="4" class="item item4 gainsboro">a.</div>
<!--
-->
<div id="5" class="item item5 coral">a.</div>
<!--
-->
<div id="6" class="item item6 crimson">a.</div>
<!--
-->
<div id="7" class="item item7 darkgoldenrod">a.</div>
<!--
-->
<div id="8" class="item item8 aquamarine">a.</div>
<!--
-->
<div id="9" class="item item9 aquamarine">a.</div>
<!--
-->
<div id="10" class="item item10 aquamarine">a.</div>
<!--
-->
<div id="11" class="item item11 aquamarine">a.</div>
<!--
--><a class="next-arrow" href=""><</a>
</div>
您可以使用 css 过渡或 css 动画。动画如:
- 定义 css class 规则并为要设置动画的元素指定 class 名称
.doFadeIn {
-webkit-animation:fadeInSlide forwards ease-in 150ms;
animation:fadeInSlide forwards ease-in 150ms;
}
.doFadeOut {
-webkit-animation:fadeOutSlideDown forwards ease-out 150ms;
animation:fadeOutSlideDown forwards ease-out 150ms;
}
- 定义动画的关键帧
@keyframes fadeInSlide
{
0% { opactity:0; transform: translateX(-100px); }
100% { opactity:1; transform: translateX(0); }
}
将 doFadeIn 分配给元素的 class 名称时,它们将: 淡入 - opacity:0 到 1(0% 可见到 100% 可见) 沿元素原始位置左侧的水平轴 (X) 移动 100px 到它的原始位置。
它将在 150 毫秒的持续时间内执行此操作
<div id="my-widget1" class="my-widget"><div>
<div id="my-widget1" class="my-widget doFadeIn"><div>
如果你只想使用show()和hide(),你可以使用show('slow')和hide('slow')等。除此之外还有很多方法和效果你可以使用。
请按照此 link 了解有多少种方法可以做到这一点。
您可以为 show()
函数传递一个参数,例如 .show('slow')
,它将像 fadeIn()
函数一样执行动画。
检查 documentation 示例
您可以使用 .hide()
、fadeOut()
、show()
、fadeIn()
示例代码
您可以使用.show()
来显示匹配的元素。
用户在输入文本中键入内容时显示列表的示例代码。
$("#search-guide").focusin(function () {
$("#list-user-guides").show();
});
您可以使用.hide()
来隐藏匹配的元素。
当用户单击屏幕上的其他地方时隐藏列表的示例代码。
$("#search-guide").focusin(function () {
$("#list-user-guides").hide();
});
但我个人使用 .fadeIn()
和 fadeOut()
因为这两种方法都带有流畅的动画。
上面的回答可以查看官方文档