简体   繁体   English

使用两个相同的JavaScript

[英]Using two of the same javascripts

I've been trying to create a page with several before and after images (Using a slider to swap between the two). 我一直在尝试创建一个包含多个前后图像的页面(使用滑块在两者之间进行交换)。

However when I add the second piece of JavaScript code, it breaks the page. 但是,当我添加第二段JavaScript代码时,它将中断页面。 Even if I try to amend the (var) code to be unique from the previous script 即使我尝试将(var)代码修改为与先前脚本唯一的

In all honesty I don't quite understand what the JavaScript is doing which is why I'm probably unable to Google the solution. 老实说,我不太了解JavaScript在做什么,这就是为什么我可能无法使用Google解决方案的原因。 Any help would be appreciated, if you could try to explain in as much detail what I need to do and explain any specific terms that would help me develop further. 如果您可以尝试尽可能详细地解释我需要做的事情,并解释任何可以帮助我进一步发展的特定术语,我们将不胜感激。

You can see all my code on the link (and below): http://codepen.io/sn0wm0nkey/pen/DakbA 您可以在链接上(和下面)看到我的所有代码: http : //codepen.io/sn0wm0nkey/pen/DakbA

 var inkbox = document.getElementById("inked-painted"); var colorbox = document.getElementById("colored"); var fillerImage = document.getElementById("inked"); inkbox.addEventListener("mousemove",trackLocation,false); inkbox.addEventListener("touchstart",trackLocation,false); inkbox.addEventListener("touchmove",trackLocation,false); function trackLocation(e) { var rect = inked.getBoundingClientRect(); var position = ((e.pageX - rect.left) / inked.offsetWidth)*100; if (position <= 100) { colorbox.style.width = position+"%"; } } /* -----second JavaScript code---- */ var inkbox1 = document.getElementById("inked1-painted"); var colorbox1 = document.getElementById("colored1"); var fillerImage1 = document.getElementById("inked1"); inkbox1.addEventListener("mousemove",trackLocation,false); inkbox1.addEventListener("touchstart",trackLocation,false); inkbox1.addEventListener("touchmove",trackLocation,false); function trackLocation(e1) { var rect1 = inked.getBoundingClientRect(); var position1 = ((e1.pageX - rect1.left) / inked1.offsetWidth)*100; if (position1 <= 100) { colorbox1.style.width = position1+"%"; } } 
 body { background: #113; } div#inked-painted { position: relative; font-size: 0; -ms-touch-action: none; -webkit-touch-callout: none; -webkit-user-select: none; } div#inked-painted img { width: 100%; height: auto; } div#colored { background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/colored-panel.jpg); position: absolute; top: 0; left: 0; height: 100%; width: 50%; background-size: cover; } div#inked-painted:hover { cursor: col-resize; } /*-------- second css sheet --------- */ div#inked1-painted { position: relative; font-size: 0; -ms-touch-action: none; -webkit-touch-callout: none; -webkit-user-select: none; } div#inked1-painted img { width: 100%; height: auto; } div#colored1 { position: absolute; top: 0; left: 0; height: 100%; width: 50%; background-size: cover; background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/colored-panel.jpg); } div#inked1-painted:hover { cursor: col-resize; } 
 <Div> <div id="inked-painted"> <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/inked-panel.png" id="inked" alt> <div id="colored"></div> </div> <p></p> <div> <div id="inked1-painted"> <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/inked-panel.png" id="inked1" alt> <div id="colored1"></div> </div> 

First, Java != JavaScript. 首先,Java!= JavaScript。 They are two very different languages. 他们是两种截然不同的语言。

Second, your issue is that your second function is named the same as your first function. 其次,您的问题是第二个函数的名称与第一个函数的名称相同。 The second one essentially overwrites the first one, so the first doesn't exist any longer. 第二个基本上覆盖了第一个,因此第一个不再存在。 Simply use a different name for your second function, and your code works just fine. 只需为第二个函数使用一个不同的名称,您的代码就可以正常工作。

However, it would be better to find a way to reuse your first function, instead of having two almost identical functions. 但是,最好找到一种方法来重用第一个函数,而不要使用两个几乎完全相同的函数。

Here is what you are doing with your JavaScript how it is currently written. 这是您当前使用JavaScript编写的方式。

  1. Declare and assign variables inkbox, colorbox, fillerImage 声明并分配变量inkbox,colorbox,fillerImage
  2. Add event handlers 添加事件处理程序
  3. Create a function in the global scope by the name of trackLocation 通过trackLocation的名称在全局范围内创建一个函数
  4. Declare and assign variables inkbox1, colorbox1, fillerImage1 声明并分配变量inkbox1,colorbox1,fillerImage1
  5. Add event handlers 添加事件处理程序
  6. Overwrite the trackLocation function in the global scope 在全局范围内覆盖trackLocation函数

All of this is being done synchronously, just as I have it listed here. 所有这些都是同步完成的,就像我在这里列出的一样。 So, when an event fires on inkbox , it calls the new function that overwrote the original. 因此,当事件在inkboxinkbox ,它将调用覆盖原始函数的新函数。

Another problem that I see (unless you omitted some code) is you have some variables that are not defined, which will cause a problem within your function. 我看到的另一个问题(除非您省略了一些代码)是您有一些未定义的变量,这将在函数中引起问题。

function trackLocation (e) {
    // inked is undefined
    var rect = inked.getBoundingClientRect();

    // inked is undefined
    var position = ((e.pageX - rect.left) / inked.offsetWidth)*100;

    if (position <= 100) { colorbox.style.width = position+"%"; }
}

You'll need to rewrite your function to accept local variables like this: 您需要重写函数以接受局部变量,如下所示:

function trackLocation (e, inked, colorbox) {
    var rect = inked.getBoundingClientRect();
    var position = ((e.pageX - rect.left) / inked.offsetWidth)*100;
    if (position <= 100) { colorbox.style.width = position+"%"; }
}

Now this one function can be reused in all of your event handlers, like this: 现在,可以在所有事件处理程序中重用此功能,如下所示:

function trackLocation (e, inked, colorbox) {
    var rect = inked.getBoundingClientRect();
    var position = ((e.pageX - rect.left) / inked.offsetWidth)*100;
    if (position <= 100) { colorbox.style.width = position+"%"; }
}


var inkbox = document.getElementById("inked-painted");
var colorbox = document.getElementById("colored");
var fillerImage = document.getElementById("inked");
inkbox.addEventListener("mousemove", function (e) { trackLocation(e, inkbox, colorbox); });
inkbox.addEventListener("touchstart", function (e) { trackLocation(e, inkbox, colorbox); });
inkbox.addEventListener("touchmove", function (e) { trackLocation(e, inkbox, colorbox); });

var inkbox1 = document.getElementById("inked1-painted");
var colorbox1 = document.getElementById("colored1");
var fillerImage1 = document.getElementById("inked1");
inkbox1.addEventListener("mousemove", function (e) { trackLocation(e, inkbox1, colorbox1); });
inkbox1.addEventListener("touchstart", function (e) { trackLocation(e, inkbox1, colorbox1); });
inkbox1.addEventListener("touchmove", function (e) { trackLocation(e, inkbox1, colorbox1); });

Howard's solution works but can be improved even more to remove duplication. 霍华德的解决方案行之有效,但可以进一步改进以消除重复。

  • Use classes instead of Ids 使用类而不是ID
  • Find the elements inside the div that is receiving the mousemove instead of passing them in 在div中找到正在接受鼠标移动的元素,而不是将其传递
  • Don't duplicate the CSS 不要复制CSS

 function onMouseMove(e) { var inked = this.getElementsByTagName("img")[0]; var colorbox = this.querySelector('.colored'); var rect = inked.getBoundingClientRect(); var position = ((e.pageX - rect.left) / inked.offsetWidth) * 100; if (position <= 100) { colorbox.style.width = position + "%"; } } function trackLocation(el) { el.addEventListener("mousemove", onMouseMove, false); el.addEventListener("touchstart", onMouseMove, false); el.addEventListener("touchmove", onMouseMove, false); } var wrappers = document.querySelectorAll('.inked-painted'); for (var i = 0; i < wrappers.length; i++) { trackLocation(wrappers[i]); } 
 div.inked-painted { position: relative; font-size: 0; -ms-touch-action: none; -webkit-touch-callout: none; -webkit-user-select: none; } div.inked-painted img { width: 100%; height: auto; } .colored { background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/colored-panel.jpg); position: absolute; top: 0; left: 0; height: 100%; width: 50%; background-size: cover; } div.inked-painted:hover { cursor: col-resize; } 
 <div class="inked-painted"> <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/inked-panel.png" /> <div class="colored"></div> </div> <p></p> <div class="inked-painted"> <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/inked-panel.png" /> <div class="colored"></div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM