我想在我的MVC应用程序中尝试使用Rellax(javascript parallax库)。
所以我在目标视图的@section scripts{<--->}
添加了rellax.js脚本链接(没有捆绑)和几行脚本。
现在当我尝试运行控制台响应时抛出错误
未捕获错误:您尝试选择的元素不存在。 在新g(rellax.min.js:1)
我在Rellax Github阅读了类似的问题,他说使用rellax.js而不是每页捆绑。但我在一个视图中使用了rellax.js和标签而没有捆绑 这是我的.cshtml
<div class="rellax-header rellax-header-sky" data-rellax-speed="-8" style="transform: translate3d(0px, 421px, 0px);">
<div class="page-header-image" style="background-image:url(@backPath)">
</div>
</div>
<div class="rellax-header rellax-header-buildings" data-rellax-speed="0" style="transform: translate3d(0px, 0px, 0px);">
<div class="page-header-image page-header-city" style="background-image:url(@imagePath)">
</div>
</div>
<div class="rellax-text-container rellax-text" style="transform: translate3d(0px, 128px, 0px);">
<h1 class="h1-seo" data-rellax-speed="-2">Test</h1>
<div class="pro">PRO</div>
</div>
@section Scripts {
<script src="~/js/plugins/rellax.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
if ($(window).width() >= 991) {
setTimeout(function () {
var rellax = new Rellax('.rellax', {
center: true
});
}, 5000);
var rellaxHeader = new Rellax('.rellax-header');
var rellaxText = new Rellax('.rellax-text');
}
});
</script>
}
我哪里错了? 有什么想法吗?!
答案 0 :(得分:1)
我能够在本地复制错误。如果您更改脚本标记以使用完整版本的rellax.js:
<script src="~/js/plugins/rellax.js" type="text/javascript"></script>
它也会给你一个类似的堆栈跟踪:
rellax.js:100 Uncaught Error: The elements you're trying to select
don't exist.
at new Rellax (rellax.js:100)
at Index:68
索引:68对我来说是:
var rellax = new Rellax('.rellax', {
这是因为没有一个元素属于&#39; rellax&#39;
为了解决这个错误,我也更改了第一个div:
<div class="rellax rellax-header-sky" data-rellax-speed="-8">
所以有一个元素有一个&#39; rellax&#39;