comparision.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. class BeforeAfter {
  2. constructor(enteryObject) {
  3. const beforeAfterContainer = document.querySelector(enteryObject.id);
  4. const before = beforeAfterContainer.querySelector('.bal-before');
  5. const afterText = beforeAfterContainer.querySelector('.bal-afterPosition');
  6. const handle = beforeAfterContainer.querySelector('.bal-handle');
  7. var widthChange = 0;
  8. beforeAfterContainer.querySelector('.bal-before-inset').setAttribute("style", "width: " + beforeAfterContainer.offsetWidth + "px;")
  9. window.onresize = function () {
  10. beforeAfterContainer.querySelector('.bal-before-inset').setAttribute("style", "width: " + beforeAfterContainer.offsetWidth + "px;")
  11. }
  12. before.setAttribute('style', "width: 50%;");
  13. handle.setAttribute('style', "left: 50%;");
  14. //touch screen event listener
  15. beforeAfterContainer.addEventListener("touchstart", (e) => {
  16. beforeAfterContainer.addEventListener("touchmove", (e2) => {
  17. let containerWidth = beforeAfterContainer.offsetWidth;
  18. let currentPoint = e2.changedTouches[0].clientX;
  19. let startOfDiv = beforeAfterContainer.offsetLeft;
  20. let modifiedCurrentPoint = currentPoint - startOfDiv;
  21. if (modifiedCurrentPoint > 10 && modifiedCurrentPoint < beforeAfterContainer.offsetWidth - 10) {
  22. let newWidth = modifiedCurrentPoint * 100 / containerWidth;
  23. before.setAttribute('style', "width:" + newWidth + "%;");
  24. handle.setAttribute('style', "left:" + newWidth + "%;");
  25. }
  26. });
  27. });
  28. //mouse move event listener
  29. beforeAfterContainer.addEventListener('mousemove', (e) => {
  30. let containerWidth = beforeAfterContainer.offsetWidth;
  31. widthChange = e.offsetX;
  32. let newWidth = widthChange * 100 / containerWidth;
  33. if (e.offsetX > 10 && e.offsetX < beforeAfterContainer.offsetWidth - 10) {
  34. before.setAttribute('style', "width:" + newWidth + "%;");
  35. handle.setAttribute('style', "left:" + newWidth + "%;");
  36. }
  37. })
  38. }
  39. }