Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

javascript - GSAP and querySelectorAll not firing on dynamically added fields

I am trying out code from this codepen https://codepen.io/aaroniker/pen/WNxoovJ and it works great on an input field inside a div with class url-input as shown in the code pen. However, when I place it all in a div with id wrapper and I add a jquery-clone function to duplicate the div.url-input, it successfully duplicates the HTML but all the functionality stops working for the dynamically added field. I am using jQuery in the rest of the page, but this codepen uses plain javascript.

I suspect this is also a problem with the event listeners inside the forEach loop. Here is what I have tried: Instead of document.querySelectorAll('.url-input').forEach(elem => { on line 21, I have tried

document.querySelectorAll('#wrapper').forEach(elem => {

and then lines 23, 23,25 and 27, I have used a descendant selector e.g. elem.querySelector('.url-input .icon') rather than elem.querySelector('.icon')

When I tried the above, even the initial url-input field stopped working, and the dynamic ones are not firing either. How would I incorporate this code into a wrapper where multiple url-input divs can be dynamically added?

EDIT: here is all the code, including the script for the cloning process. The "template" tag contains a minified version of the HTML template for the url input field, for easy cloning purposes. I also minified the CSS styles, to save space here.

<html>
<head>
<style>
<!-- From https://codepen.io/aaroniker/pen/WNxoovJ -->
.url-input{--background: #fff; --border-default: #E1E6F9; --border-active: #275EFE; --text-color: #646B8C; --placeholder-color: #BBC1E1; --icon: #275EFE; --close: #646B8C; --close-light: #A6ACCD; --close-background: #EFF2FB; width: 100%; max-width: 240px; display: flex; align-items: center; position: relative; border-radius: 8px; background: var(--background); box-shadow: inset 0 0 0 var(--border-width, 1px) var(--border, var(--border-default)); transition: box-shadow 0.2s; --favicon-scale: 0; --icon-offset: 0px; --clear-x: 0px; --clear-swipe-left: 0px; --clear-swipe-x: 0; --clear-swipe: 0px; --clear-scale: 0; --clear-rotate: 0deg; --clear-opacity: 0; --clear-arrow-o: 1; --clear-arrow-x: 0px; --clear-arrow-y: 0px; --clear-arrow-offset: 4px; --clear-arrow-offset-second: 4px; --clear-line-array: 8.5px; --clear-line-offset: 27px; --clear-long-array: 8.5px; --clear-long-offset: 24px;}.url-input.clearing, .url-input:focus-within{--border-width: 1.5px; --border: var(--border-active);}.url-input.clearing{--close-background: transparent; --clear-arrow-stroke: var(--close-light);}.url-input .icon{position: absolute; left: 15px; top: 15px; pointer-events: none;}.url-input .icon svg,.url-input .icon img{display: block; width: 18px; height: 18px;}.url-input .icon svg{fill: none; stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; stroke: var(--icon);}.url-input .icon svg path{stroke-dasharray: 24px; stroke-dashoffset: var(--icon-offset);}.url-input .icon .favicon{position: absolute; left: 0; top: 0; transform: scale(var(--favicon-scale)) translateZ(0);}.url-input .text{flex-grow: 1;}.url-input .text input{-webkit-appearance: none; line-height: 24px; background: none; border: none; outline: none; display: block; width: 100%; margin: 0; padding: 12px 12px 12px 44px; font-family: inherit; font-size: 14px; font-weight: 500; color: var(--text-color);}.url-input .text input::-moz-placeholder{color: var(--placeholder-color);}.url-input .text input:-ms-input-placeholder{color: var(--placeholder-color);}.url-input .text input::placeholder{color: var(--placeholder-color);}.url-input .clear{-webkit-appearance: none; position: relative; z-index: 1; padding: 0; margin: 12px 12px 12px 0; border: none; outline: none; background: var(--b, transparent); transition: background 0.2s; border-radius: 6px; opacity: var(--clear-opacity); transform: scale(var(--clear-scale)) translateZ(0);}.url-input .clear:before{content: ""; position: absolute; top: 0; bottom: 0; right: 12px; left: var(--clear-swipe-left); background: var(--background); transform-origin: 100% 50%; transform: translateX(var(--clear-swipe)) scaleX(var(--clear-swipe-x)) translateZ(0);}.url-input .clear svg{display: block; position: relative; z-index: 1; width: 24px; height: 24px; outline: none; cursor: pointer; fill: none; stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; stroke: var(--close); transform: translateX(var(--clear-x)) rotate(var(--clear-rotate)) translateZ(0);}.url-input .clear svg path{transition: stroke 0.2s;}.url-input .clear svg path.arrow{stroke: var(--clear-arrow-stroke, var(--close)); stroke-dasharray: 4px; stroke-dashoffset: var(--clear-arrow-offset); opacity: var(--clear-arrow-o); transform: translate(var(--clear-arrow-x), var(--clear-arrow-y)) translateZ(0);}.url-input .clear svg path.arrow:last-child{stroke-dashoffset: var(--clear-arrow-offset-second);}.url-input .clear svg path.line{stroke-dasharray: var(--clear-line-array) 28.5px; stroke-dashoffset: var(--clear-line-offset);}.url-input .clear svg path.long{stroke: var(--clear-arrow-stroke, var(--close)); stroke-dasharray: var(--clear-long-array) 15.5px; stroke-dashoffset: var(--clear-long-offset); opacity: var(--clear-arrow-o); transform: translate(var(--clear-arrow-x), var(--clear-arrow-y)) translateZ(0);}.url-input .clear:hover{--b: var(--close-background);}
</style>
</head>

<body>
<!-- Modified from https://codepen.io/aaroniker/pen/WNxoovJ -->
<div id="wrapper">
    <a href="javascript:;" id="cloner">Make copy</a>
    <div class="url-input">
        <div class="icon">
            <svg viewBox="0 0 18 18">
                <path d="M10.05 7.95001C11.55 9.45001 11.55 11.775 10.05 13.275L7.95 15.375C6.45 16.875 4.125 16.875 2.625 15.375C1.125 13.875 1.125 11.55 2.625 10.05L4.5 8.25001" />
                <path d="M7.9502 10.05C6.4502 8.55 6.4502 6.225 7.9502 4.725L10.0502 2.625C11.5502 1.125 13.8752 1.125 15.3752 2.625C16.8752 4.125 16.8752 6.45 15.3752 7.95L13.5002 9.75" />
            </svg>
            <div class="favicon"></div>
        </div>
        <div class="text">
            <input type="text" placeholder="Your URL" />
        </div>
        <button class="clear">
            <svg viewBox="0 0 24 24">
                <path class="line" d="M2 2L22 22" />
                <path class="long" d="M9 15L20 4" />
                <path class="arrow" d="M13 11V7" />
                <path class="arrow" d="M17 11H13" />
            </svg>
        </button>
    </div>
</div>

<template id="url-template" style="display:none;">
    <div class="url-input"> <div class="icon"> <svg viewBox="0 0 18 18"> <path d="M10.05 7.95001C11.55 9.45001 11.55 11.775 10.05 13.275L7.95 15.375C6.45 16.875 4.125 16.875 2.625 15.375C1.125 13.875 1.125 11.55 2.625 10.05L4.5 8.25001"/> <path d="M7.9502 10.05C6.4502 8.55 6.4502 6.225 7.9502 4.725L10.0502 2.625C11.5502 1.125 13.8752 1.125 15.3752 2.625C16.8752 4.125 16.8752 6.45 15.3752 7.95L13.5002 9.75"/> </svg> <div class="favicon"></div></div><div class="text"> <input type="text" placeholder="Your URL"/> </div><button class="clear"> <svg viewBox="0 0 24 24"> <path class="line" d="M2 2L22 22"/> <path class="long" d="M9 15L20 4"/> <path class="arrow" d="M13 11V7"/> <path class="arrow" d="M17 11H13"/> </svg> </button> </div>
</template>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.0.5/gsap.min.js"></script>

<script>
// From https://codepen.io/aaroniker/pen/WNxoovJ
const { to, set, timeline } = gsap

function validURL(str) {
    let pattern = new RegExp('^(https?:\/\/)?'+
        '((([a-z\d]([a-z\d-]*[a-z\d])*)\.)+[a-z]{2,}|'+
        '((\d{1,3}\.){3}\d{1,3}))'+
        '(\:\d+)?(\/[-a-z\d%_.~+]*)*'+
        '(\?[;&a-z\d%_.~+=-]*)?'+
        '(\#[-a-z\d_]*)?$','i')
    return !!pattern.test(str)
}

function delay(fn, ms) {
    let timer = 0
    return function(...args) {
        clearTimeout(timer)
        timer = setTimeout(fn.bind(this, ...args), ms || 0)
    }
}

document.querySelectorAll('.url-input').forEach(elem => {
    let icon = elem.querySelector('.icon'),
        favicon = icon.querySelector('.favicon'),
        clear = elem.querySelector('.clear'),
        input = elem.querySelector('input'),
        { classList } = elem,
        svgLine = clear.querySelector('.line'),
        svgLineProxy = new Proxy({
            x: null
        }, {
            set(target, key, value) {
                target[key] = value
                if(target.x !== null) {
                    svgLine.setAttribute('d', getPath(target.x, .1925))
                }
                return true
            },
            get(target, key) {
                return target[key]
            }
        })

    svgLineProxy.x = 0

    input.addEventListener('input', delay(e => {
        let bool = input.value.length,
            valid = validURL(input.value)
        to(elem, {
            '--clear-scale': bool ? 1 : 0,
            duration: bool ? .5 : .15,
            ease: bool ? 'elastic.out(1, .7)' : 'none'
        })
        to(elem, {
            '--clear-opacity': bool ? 1 : 0,
            duration: .15
        })
        to(elem, {
            '--icon-offset': valid ? '24px' : '0px',
            duration: .15,
            delay: valid ? 0 : .2
        })
        if(valid) {
            if(favicon.querySelector('img')) {
                favicon.querySelector('img').src = 'https://f1.allesedv.com/64/' + input.value
                return
            }
            let img = new Image()
            img.onload = () => {
                favicon.appendChild(img)
                to(elem, {
                    '--favicon-scale': 1,
                    duration: .5,
                    delay: .2,
                    ease: 'elastic.out(1, .7)'
                })
            }
            img.src = 'https://f1.allesedv.com/64/' + input.value
        } else {
            if(favicon.querySelector('img')) {
                to(elem, {
                    '--favicon-scale': 0,
                    duration: .15,
                    onComplete() {
                        favicon.querySelector('img').remove()
                    }
                })
            }
        }
    }, 250))

    clear.addEventListener('click', e => {
        classList.add('clearing')
        set(elem, {
            '--clear-swipe-left': (input.offsetWidth - 44) * -1 + 'px'
        })
        to(elem, {
            keyframes: [{
                '--clear-rotate': '45deg',
                duration: .25
            }, {
                '--clear-arrow-x': '2px',
                '--clear-arrow-y': '-2px',
                duration: .15
            }, {
                '--clear-arrow-x': '-3px',
           

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

1 Answer

0 votes
by (71.8m points)

the document.querySelectorAll is just called one time at the launch of script, so you have to execute again each time you create a new input.

So to avoid to rewrite your coding, you just encapsulate the initialisation of querySelectorAll in a function

function initInput() {
  document.querySelectorAll(".url-input").forEach((elem) => {
  :
  :
        to(elem, {
          "--clear-arrow-offset": "0px",
          "--clear-arrow-offset-second": "8px",
          "--clear-line-array": "28.5px",
          "--clear-line-offset": "57px",
          "--clear-long-offset": "17px",
          duration: 0.2,
        });
      });//end clear.addEventListener
    });//end queryselectorAll
}

and you call it at the beginning of script and in the cloner:

jQuery(function () {

  const { to, set, timeline } = gsap;
  initInput();
  $("#cloner").click(function (e) {
    e.preventDefault();
    let $urlTemplate = $("template#url-template").html();
    let urlCopy = $($urlTemplate).clone();
    $("#wrapper").append(urlCopy);
    $(this).blur();
    initInput();
  });
  :
  :

const { to, set, timeline } = gsap;
  initInput();
  $("#cloner").click(function (e) {
    e.preventDefault();
    let $urlTemplate = $("template#url-template").html();
    let urlCopy = $($urlTemplate).clone();
    $("#wrapper").append(urlCopy);
    $(this).blur();
    initInput();
  });

  function validURL(str) {
    let pattern = new RegExp(
      "^(https?:\/\/)?" +
        "((([a-z\d]([a-z\d-]*[a-z\d])*)\.)+[a-z]{2,}|" +
        "((\d{1,3}\.){3}\d{1,3}))" +
        "(\:\d+)?(\/[-a-z\d%_.~+]*)*" +
        "(\?[;&a-z\d%_.~+=-]*)?" +
        "(\#[-a-z\d_]*)?$",
      "i"
    );
    return !!pattern.test(str);
  }

  function delay(fn, ms) {
    let timer = 0;
    return function (...args) {
      clearTimeout(timer);
      timer = setTimeout(fn.bind(this, ...args), ms || 0);
    };
  }

  function initInput() {
    document.querySelectorAll(".url-input").forEach((elem) => {

      let icon = elem.querySelector(".icon"),
        favicon = icon.querySelector(".favicon"),
        clear = elem.querySelector(".clear"),
        input = elem.querySelector("input"),
        { classList } = elem,
        svgLine = clear.querySelector(".line"),
        svgLineProxy = new Proxy(
          {
            x: null,
          },
          {
            set(target, key, value) {
              target[key] = value;
              if (target.x !== null) {
                svgLine.setAttribute("d", getPath(target.x, 0.1925));
              }
              return true;
            },
            get(target, key) {
              return target[key];
            },
          }
        );

      svgLineProxy.x = 0;

      input.addEventListener(
        "input",
        delay((e) => {
          let bool = input.value.length,
            valid = validURL(input.value);
          to(elem, {
            "--clear-scale": bool ? 1 : 0,
            duration: bool ? 0.5 : 0.15,
            ease: bool ? "elastic.out(1, .7)" : "none",
          });
          to(elem, {
            "--clear-opacity": bool ? 1 : 0,
            duration: 0.15,
          });
          to(elem, {
            "--icon-offset": valid ? "24px" : "0px",
            duration: 0.15,
            delay: valid ? 0 : 0.2,
          });
          if (valid) {
            if (favicon.querySelector("img")) {
              favicon.querySelector("img").src =
                "https://f1.allesedv.com/64/" + input.value;
              return;
            }
            let img = new Image();
            img.onload = () => {
              favicon.appendChild(img);
              to(elem, {
                "--favicon-scale": 1,
                duration: 0.5,
                delay: 0.2,
                ease: "elastic.out(1, .7)",
              });
            };
            img.src = "https://f1.allesedv.com/64/" + input.value;
          } else {
            if (favicon.querySelector("img")) {
              to(elem, {
                "--favicon-scale": 0,
                duration: 0.15,
                onComplete() {
                  favicon.querySelector("img").remove();
                },
              });
            }
          }
        }, 250)
      );

      clear.addEventListener("click", (e) => {
        classList.add("clearing");
        set(elem, {
          "--clear-swipe-left": (input.offsetWidth - 44) * -1 + "px",
        });
        to(elem, {
          keyframes: [
            {
              "--clear-rotate": "45deg",
              duration: 0.25,
            },
            {
              "--clear-arrow-x": "2px",
              "--clear-arrow-y": "-2px",
              duration: 0.15,
            },
            {
              "--clear-arrow-x": "-3px",
              "--clear-arrow-y": "3px",
              "--clear-swipe": "-3px",
              duration: 0.15,
              onStart() {
                to(svgLineProxy, {
                  x: 3,
                  duration: 0.1,
                  delay: 0.05,
                });
              },
            },
            {
              "--clear-swipe-x": 1,
              "--clear-x": (input.offsetWidth - 32) * -1 + "px",
              duration: 0.45,
              onComplete() {
                input.value = "";
                input.focus();
                if (favicon.querySelector("img")) {
                  to(elem, {
                    "--favicon-scale": 0,
                    duration: 0.15,
                    onComplete() {
                      favicon.querySelector("img").remove();
                    },
                  });
                  to(elem, {
                    "--icon-offset": "0px",
                    "--icon-offset-line": "0px",
                    duration: 0.15,
                    delay: 0.2,
                  });
                }
                to(elem, {
                  "--clear-arrow-offset": "4px",
                  "--clear-arrow-offset-second": "4px",
                  "--clear-line-array": "8.5px",
                  "--clear-line-offset": "27px",
                  "--clear-long-offset": "24px",
                  "--clear-rotate": "0deg",
                  "--clear-arrow-o": 1,
                  duration: 0,
                  delay: 0.7,
                  onStart() {
                    classList.remove("clearing");
                  },
                });
                to(elem, {
                  "--clear-opacity": 0,
                  duration: 0.2,
                  delay: 0.55,
                });
                to(elem, {
                  "--clear-arrow-o": 0,
                  "--clear-arrow-x": "0px",
                  "--clear-arrow-y": "0px",
                  "--clear-swipe": "0px",
                  duration: 0.15,
                });
                to(svgLineProxy, {
                  x: 0,
                  duration: 0.45,
                  ease: "elastic.out(1, .75)",
                });
              },
            },
            {
              "--clear-swipe-x": 0,
              "--clear-x": "0px",
              duration: 0.4,
              delay: 0.35,
            },
          ],
        });
        to(elem, {
          "--clear-arrow-offset": "0px",
          "--clear-arrow-offset-second": "8px",
          "--clear-line-array": "28.5px",
          "--clear-line-offset": "57px",
          "--clear-long-offset": "17px",
          duration: 0.2,
        });
      });
    });
  }

  function getPoint(point, i, a, smoothing) {
    let cp = (current, previous, next, reverse) => {
        let p = previous || current,
          n = next || current,
          o = {
            length: Math.sqrt(
              Math.pow(n[0] - p[0], 2) + Math.pow(n[1] - p[1], 2)
            ),
            angle: Math.atan2(n[1] - p[1], n[0] - p[0]),
          },
          angle = o.angle + (reverse ? Math.PI : 0),
          length = o.length * smoothing;
        return [
          current[0] + Math.cos(angle) * length,
          current[1] + Math.sin(angle) * length,
        ];
      },
      cps = cp(a[i - 1], a[i - 2], point, false),
      cpe = cp(point, a[i - 1], a[i + 1], true);
    return `C ${cps[0]},${cps[1]} ${cpe[0]},${cpe[1]} ${point[0]},${point[1]}`;
  }

  function getPath(x, smoothing) {
    return [
      [2, 2],
      [12 - x, 12 + x],
      [22, 22],
    ].reduce(
      (acc, point, i, a) =>
        i === 0
          ? `M ${point[0]},${point[1]}`
          : `${acc} ${getPoint(point, i, a, smoothing)}`,
      ""
    );
  }
.url-input {
    --background: #fff;
    --border-default: #E1E6F9;
    --border-active: #275EFE;
    --text-color: #646B8C;
    --placeholder-color: #BBC1E1;
    --icon: #275EFE;
    --close: #646B8C;
    --close-light: #A6ACCD;
    --close-background: #EFF2FB;
    width: 100%;
    max-width: 240px;
    display: flex;
    align-items: center;
    position: relative;
    border-radius: 8px;
    background: var(--background);
    box-shadow: inset 0 0 0 var(--border-width, 1px) var(--border, var(--border-default));
    transition: box-shadow 0.2s;
    --favicon-scale: 0;
    --icon-offset: 0px;
    --clear-x: 0px;
    --clear-swipe-left: 0px;
    --clear-swipe-x: 0;
    --clear-swipe: 0px;
    --clear-scale: 0;
    --clear-rotate: 0deg;
    --clear-opacity: 0;
    --clear-arrow-o: 1;
    --clear-arrow-x: 0px;
    --clear-arrow-y: 0px;
    --clear-arrow-offset: 4px;
    --clear-arrow-offset-second: 4px;
    --clear-line-array: 8.5px;
    --clear-line-offset: 27px;
    --clear-long-array: 8.5px;
    --clear-long-offset: 24px;
  }
  
  .url-input.clearing, .url-input:focus-within {
    --border-width: 1.5px;
    --border: var(--border-active);
  }
  
  .url-input.clearing {
    --close-background: transparent;
    --clear-arrow-stroke: var(--close-light);
  }
  
  .url-input .icon {
    position: absolute;
    left: 15px;
    top: 15px;
    pointer-events: none;
  }
  
  .url-input .icon svg, .url-input .icon img {
    display: block;
    width: 18px;
    height: 18px;
  }
  
  .url-input .icon svg {
    fill: none;
    stroke-width: 1.5;
    stroke-linecap: round;
    stroke-linejoin: round;
    stroke: var(--icon);
  }
  
  .url-input .icon svg path {
    stroke-dasharray: 24px;
    stroke-dashoffset: var(--icon-offset);
  }
  
  .url-input .icon .favicon {
    position: absolute;
    left: 0;
    top: 0;
    transform: scale(var(--favicon-scale)) translateZ(0);
  }
  
  .url-input .text {
    flex-grow: 1;
  }
  
  .url-input .text input {
    -webkit-appearance: none;
    line-height: 24px;
    background: none;
    border: none;
    outline: none;
    display: block;
    width: 100%;
    margin: 0;
    padding: 12px 12px 12px 44px;
    font-family: inherit;
    font-size: 14px;
    font-weight: 500;
    color: var(--text-color);
  }
  
  .url-input .text input::-moz-placeholder {
    color: var(--placeholder-color);
  }
  
  .url-input .text input:-ms-input-placeholder {
    color: var(--placeholder-color);
  }
  
  .url-input .text input::placeholder {
    color: var(--placeholder-color);
  }
  
  .url-input .clear {
    -webkit-appearance: none;
    positi

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...