jQuery - “淡入不是一个函数”
Posted
技术标签:
【中文标题】jQuery - “淡入不是一个函数”【英文标题】:jQuery - "fadeIn is not a function" 【发布时间】:2018-01-20 14:17:25 【问题描述】:遇到一个非常奇怪的问题。尝试运行 fadeIn()
但我收到错误“.fadeIn 不是函数”。
我的代码:
body
display:none;
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<link rel="stylesheet" href="css/custom.css">
</head>
<body>
Test
<!-- Optional javascript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
<script type="text/javascript">
$(document).ready(function()
$('body').fadeIn();
)
</script>
</body>
</html>
错误信息:
"TypeError: $('body').fadeIn is not a function. (In '$('body').fadeIn()', '$('body').fadeIn' is undefined)"
使用 Bootstrap v4 beta,在 Chrome 和 Safari 上进行本地测试。
【问题讨论】:
【参考方案1】:Bootstrap 4 的默认模板使用不包含许多功能的“苗条”版本的 jQuery。您需要切换回使用功能更强大的版本。例如:
body
display:none;
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<link rel="stylesheet" href="css/custom.css">
</head>
<body>
Test
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
<script type="text/javascript">
$(document).ready(function()
$('body').fadeIn();
)
</script>
</body>
</html>
【讨论】:
Differences between normal and slim package of jquery 从来不知道有什么不同,切换文件,这帮助我解决了我在使用 Lightbox 时遇到的问题。非常感谢【参考方案2】:slim 版本用一个小的 CSS 就足够了:
jQuery("#toTop").addClass('fade-in');
和
.fade-in
opacity: 1;
animation-name: fadeInOpacity;
animation-iteration-count: 1;
animation-timing-function: ease-in;
animation-duration: 0.5s;
@keyframes fadeInOpacity
0%
opacity: 0;
100%
opacity: 1;
【讨论】:
【参考方案3】:你也可以使用 JQuery-Animation:
$(document).ready(function()
$("element-you-want-to-fadeIn").animate(
opacity: 1 ,
"slow",
function()
alert("animation complete!");
);
);
【讨论】:
以上是关于jQuery - “淡入不是一个函数”的主要内容,如果未能解决你的问题,请参考以下文章