我一直在尝试让我的应用程序不时更改背景图片,但我无法加载图片。我使用了 Inspect Element,但我的图片没有得到任何 404,但 rails 似乎无法加载它们。 到目前为止,这是我的代码。现在它只是淡入和淡出整个屏幕。
$(document).ready(function() {
var backgrounds = ["medium2-doge-hannibal.jpg", "dog_bone_wide.jpg", "water_sports_medium.jpg"];
function nextBackground() {
var rand = Math.floor(Math.random() * backgrounds.length);
$(".public-content").fadeOut(300, function () {
$(".public-content").css("background", "asset-path("+backgrounds[rand]+" )");
$(".public-content").fadeIn(300, function () {
setTimeout(nextBackground, 3000)
});
});
}
nextBackground();
});
感谢任何帮助。谢谢!
最佳答案
jvnill
是对的。
您需要 append the .erb
filetype给你的 js:
#app/assets/javascripts/application.js.erb
function nextBackground() {
var backgrounds = ["<%= asset_path('medium2-doge-hannibal.jpg') %>", "<%= asset_path('dog_bone_wide.jpg') %>", "<%= asset_path('water_sports_medium.jpg') %>"];
var rand = Math.floor(Math.random() * backgrounds.length);
$(".public-content").fadeOut(300, function () {
$(".public-content").css("background", backgrounds[rand]);
$(".public-content").fadeIn(300, function () {
setTimeout(nextBackground, 3000)
});
});
}
$(document).ready(function() {
nextBackground();
});
关于javascript - 你如何在 Rails 中改变背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33703302/