Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

javascript - 如何在Ruby应用程序的JS中定义clientWidth?(How do I define clientWidth in JS for my ruby app?)

I am having a lot of trouble getting any javascripts to run in my Ruby application.

(我很难在Ruby应用程序中运行任何JavaScript。)

Currently my console is giving me one error message:

(目前,我的控制台正在向我显示一条错误消息:)

" Uncaught TypeError: Cannot read property 'clientWidth' of undefined "

(“ 未捕获的TypeError:无法读取未定义的属性'clientWidth')

This error is referencing this line in my application.js file:

(此错误引用了我的application.js文件中的这一行:)

const size = carouselImages[0].clientWidth;

I'm guessing it's saying it has no idea what clientWidth is?

(我猜这是在说不知道clientWidth是什么?)

I was following along with this tutorial when I hit this roadblock.

(当我遇到这个障碍时,我一直在跟随本教程 。)

(He's not making a rails app, but his script loads fine.) The script also loaded fine for me when I created a regular index.html page not tied to my ruby app.

((他不是在开发Rails应用程序,但是他的脚本可以很好地加载。)当我创建一个不与我的ruby应用程序绑定的常规index.html页面时,脚本也可以很好地加载。)

How do I define clientWidth/do I need to? (如何定义clientWidth /需要?)

My Rails files:

(我的Rails文件:)

Gem File

(宝石文件)

gem 'coffee-rails', '~> 4.2'

# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

gem 'jquery-rails', '~> 4.3', '>= 4.3.3'
gem 'rails-ujs', '~> 0.1.0'
gem 'jquery-ui-rails'

_header.html.erb

(_header.html.erb)

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Page Title</title>

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

<%= javascript_include_tag( 'slider.js' ) %>

</head>
<body> <!--Body and html tags are closed in my _footer.html.erb -->

application.js

(application.js)

//= require rails-ujs
//= require jquery 
//= require jquery_ujs 
//= require turbolinks


$(document).ready to $(document).on('turbolinks:load', function(main());

//image slider code
//
//

const carouselSlide = document.querySelector('.carousel-slide');

const carouselImages = document.querySelectorAll('.carousel-slide img');


//buttons

const prevBtn = document.querySelector('#prevBtn');
const nextBtn = document.querySelector('#nextBtn');


//Counter

let counter = 1;
const size = carouselImages[0].clientWidth;

carouselSlide.style.transform = 'translateX('+(-size * counter)+ 'px)';

slider.html.erb

(slide.html.erb)

<button id="prevBtn">Prev</button>
<button id="nextBtn">Next</button>

<br /><br />

<div class="carousel-container">
<div class="carousel-slide"> 
<%= image_tag("photo4.jpg", id: "lastClone") %> 
<!-- photo1.jpg is an image of a road at sunset should have the border around it and be the focus. Currently, it is not. -->
<%= image_tag("photo1.jpg") %> 
<%= image_tag("photo2.jpg") %>
<%= image_tag("photo3.jpg") %>
<%= image_tag("photo4.jpg") %>
<%= image_tag("photo1.jpg", id: "firstClone") %>
</div>
</div>

slider.scss

(slide.scss)

/* This is for a test slider */

.carousel-container {
  width: 1000px;
  border: 5px solid black;
}

.carousel-slide {
  display: flex;

  img{
    width: 1000px;
    height: auto;
  }
}

Here is an imgur link to see a screenshot of my ruby app vs. a regular .html file I created to compare/contrast what is going on: https://imgur.com/a/jVhjLd3

(这是一个imgur链接,用于查看我的ruby应用程序和我创建的常规.html文件的屏幕截图,以比较/对比正在发生的事情: https : //imgur.com/a/jVhjLd3)

Here are the versions I am running of things:

(这是我正在运行的版本:)

Ruby : ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]

(Ruby :ruby 2.5.0p0(2017-12-25修订版61468)[x86_64-linux])

Node : v13.2.0

(节点 :v13.2.0)

NPM : 6.13.1

(NPM :6.13.1)

I tried a few different suggestions found via google searches (a la 'get fixed quick'), so if anything is off or out of place, please point it out to me.

(我尝试了一些通过Google搜索找到的不同建议(“快速修复问题”),因此,如果有任何异常或异常之处,请向我指出。)

If you require more info/code, I will be happy to provide.

(如果您需要更多信息/代码,我们将很乐意提供。)

Any help getting my Javascript to run would be greatly appreciated!

(任何帮助我运行Javascript的帮助将不胜感激!)

  ask by madeofquartz translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It doesn't have to do with ruby but with the javascript itself.

(它与ruby无关,而与javascript本身有关。)

The problem is at querySelectorAll, it isn't returning the right value + i suggest not to use it because it isn't supported in every browser.

(问题出在querySelectorAll,它没有返回正确的值+我建议不要使用它,因为并非所有浏览器都支持它。)

So just add a class to each image and everything would be easier :)

(因此,只需为每个图像添加一个类,一切将变得更加容易:))

A working demo for you :

(一个适合您的演示:)

<div class="carousel-slide"> 
<%= image_tag("photo4.jpg", id: "lastClone"), class: "imgCarousel" %> 
<%= image_tag("photo1.jpg"), class: "imgCarousel" %> 
<%= image_tag("photo2.jpg"), class: "imgCarousel" %>
</div>

At javascript :

(在javascript上:)

x = document.getElementsByClassName("imgCarousel")[0];
console.log(x.clientWidth)

Demo ( with JS and html ):

(演示( 带有JS和html ):)

 function myFunction() { x=document.getElementsByClassName("imgCarousel")[0]; console.log(x.clientWidth) document.getElementById("widthContainer").innerHTML = x.clientWidth } 
 <body> <img class="imgCarousel" src="https://www.w3schools.com/jsref/compman.gif" > <img class="imgCarousel" src="https://www.w3schools.com/jsref/compman.gif" > <img class="imgCarousel" src="https://www.w3schools.com/jsref/compman.gif" > <p id="widthContainer">Click the button to get clientWidth of first image.</p> <button onclick="myFunction()">Try it</button> </body> 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...