gpt4 book ai didi

Javascript 仅在放置在 View 页面中时才起作用

转载 作者:行者123 更新时间:2023-11-27 22:45:19 25 4
gpt4 key购买 nike

我的 Rails 应用程序仅在将 Js 代码放入 View 页面内的脚本标记中时才使用 Js 代码,而不是将其放入 asset js 文件夹中的 js 文件中。

这是代码:

<script type="text/javascript">
var menu = document.querySelectorAll(".side-menu")[0],
closedPosition = menu.offsetWidth - 15; //make sure there are 15 pixels showing

Draggable.create(menu, {
type:"x",
throwProps:true, //enables the momentum-based flicking (requires ThrowPropsPlugin)
edgeResistance: 0.9, //you can set this to 1 if you don't want the user to be able to drag past the snap point. This controls how much resistance there is after it hits the max/min.
maxDuration:0.3, //don't let the animation duration exceed 0.3 second (you can tweak this too of course)
bounds: {maxX:closedPosition, minX:0},
onClick: function() { //when the user clicks/taps on the menu without dragging, we'll toggle it...
if (this.target._gsTransform.x === closedPosition) {
TweenLite.to(this.target, 0.3, {x:0});
} else {
TweenLite.to(this.target, 0.3, {x:closedPosition});
}
},
snap: {
x: [0, closedPosition]
}
});
</script>

这是layouts文件夹中的application.html.erb

<!DOCTYPE html>
<html>
<head>
<title>CityScape</title>
<link href='http://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Roboto+Slab' rel='stylesheet' type='text/css'>
<meta name="viewport" content="width=device-width, initial-scale=1">

<%= javascript_include_tag 'https://cdnjs.cloudflare.com/ajax/libs/gsap/1.17.0/TweenMax.min.js'%>
<%= javascript_include_tag 'https://cdnjs.cloudflare.com/ajax/libs/gsap/1.17.0/utils/Draggable.min.js'%>


<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>

<%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>

问题是:如何让它从 Assets 中发挥作用?

最佳答案

因为,例如,您需要在 html 文件中使用 js 代码创建指向文件的链接:

<script src="/assets/js/jquery/1.7.1/jquery.min.js"></script>

关于Javascript 仅在放置在 View 页面中时才起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38446911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com