Animated ticker in javascript with clickable objects – direction issues with the svg objects

huangapple go评论77阅读模式
英文:

Animated ticker in javascript with clickable objects - direction issues with the svg objects

问题

以下是您要翻译的内容:

UPDATED 11/04-2023 

So I am trying to create an animated ticker using javascript and svg text elements however I have got something wrong with my animation <s> direction not being straight across the screen horizontally and instead it goes leftward and downward, and it only does it up in the left corner of the screen</s> this is driving me nuts!

To those suggesting I use some framework other than javascript, even libraries don't! as I am trying switch back to a "no frameworks" solution for my site as it seems no matter what you choose, all these frameworks and libraries that make your site looking all nice and lovey dovey  get hacked and then you site is in the proverbial gutter and you need to rebuild. So this time I am KISS'ing it - This is the code I have so far, it is almost there it just stops before exiting fully now.

Updated code 

        '''


        <!DOCTYPE html>
    <html>
    <head>
      <meta charset="UTF-8">
      <title>SVG Ani w/Clickable</title>
      <style>
        body, html {
          margin: 0;
          padding: 0;
          height: 100%;
          width: 100%;
        }
        #svg-container {
          position: relative;
          overflow: hidden;
          height: 100%;
          width: 100%;
          white-space: nowrap;
        }
        .svg-container {
          position: absolute;
          width: auto;
          height: 30px;
        }
        .svg {
          display: inline-block;
          cursor: pointer;
        }
        .svg text {
          fill: #333;
          font-size: 14px;
          font-family: Arial, Helvetica, sans-serif;
        }
      </style>
    </head>
    <body>
      <div id="svg-container"></div>
    
      <script>
        const links = [
          { text: 'Welcome to a soon fully revamped site - using AI to construct it - click to learn more.', href: './aiwork.html' },
        ];
    
        const duration = 9000;
        const spacing = 20;
    
        function createSVGObject(link) {
          const div = document.createElement('div');
          div.classList add ('svg-container');
    
          const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
          const text = document.createElementNS('http://www.w3.org/2000/svg', 'text');
          const a = document.createElement('a');
    
          text.setAttribute('y', '20');
          text.setAttribute('font-family', 'Arial, Helvetica, sans-serif');
          text.textContent = link.text;
          
          svg.appendChild(text);
          a.appendChild(svg);
          div.appendChild(a);
          
          a.setAttribute('href', link.href);
          a.setAttribute('class', 'svg');
          
          const computedWidth = text.getComputedTextLength();
          div.style.width = (computedWidth + 10) + 'px'
          svg.setAttribute('width', (computedWidth + 10) + 'px');
          svg.setAttribute('height', '30');
          svg.setAttribute('viewBox', `0 0 ${computedWidth + 10} 30`);
          svg.style.overflow = 'visible';
    
          return div;
        }
    
        function animateSVGObject(object) {
          object.style.top = '0';
          object.style.left = window.innerWidth + 'px';
    
          const startTime = performance.now();
          requestAnimationFrame(function animate(time) {
            const progress = (time - startTime) / duration;
            const distance = (window.innerWidth + object.getBoundingClientRect().width + spacing) * progress;
            const currentPosition = window.innerWidth - distance;
            object.style left = currentPosition + 'px';
    
            if (currentPosition + object.getBoundingClientRect().width > -object.getBoundingClientRect().width) {
              requestAnimationFrame(animate);
            } else {
              setTimeout(() => {
                object.style left = window.innerWidth + 'px';
                animateSVGObject(object);
              }, duration * links.length);
            }
          });
        }
    
        links.forEach((link, index) => {
          const svgObject = createSVGObject(link);
          document.getElementById('svg-container').appendChild(svgObject);
          setTimeout(() => animateSVGObject(svgObject), duration * index);
        });
    
      </script>
    
      <p>SVG</p>
    
    </body>
    </html>
        '''

<s>Any takes on what I am doing wrong with the directionand why the svg does not animate all the way across my screen horizontally ?  </s>

请注意,代码部分已排除在外,只提供文本内容的翻译。

英文:

UPDATED 11/04-2023

So I am trying to create an animated ticker using javascript and svg text elements however I have got something wrong with my animation <s> direction not being straight across the screen horizontally and instead it goes leftward and downward, and it only does it up in the left corner of the screen</s> this is driving me nuts!

To those suggesting I use some framework other than javascript, even libraries don't! as I am trying switch back to a "no frameworks" solution for my site as it seems no matter what you choose, all these frameworks and libraries that make your site looking all nice and lovey dovey get hacked and then you site is in the proverbial gutter and you need to rebuild. So this time I am KISS'ing it - This is the code I have so far, it is almost there it just stops before exiting fully now.

Updated code

    &#39;&#39;&#39;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;title&gt;SVG Ani w/Clickable&lt;/title&gt;
&lt;style&gt;
body, html {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
}
#svg-container {
position: relative;
overflow: hidden;
height: 100%;
width: 100%;
white-space: nowrap;
}
.svg-container {
position: absolute;
width: auto;
height: 30px;
}
.svg {
display: inline-block;
cursor: pointer;
}
.svg text {
fill: #333;
font-size: 14px;
font-family: Arial, Helvetica, sans-serif;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;svg-container&quot;&gt;&lt;/div&gt;
&lt;script&gt;
const links = [
{ text: &#39;Welcome to a soon fully revamped site - using AI to construct it - click to learn more.&#39;, href: &#39;./aiwork.html&#39; },
];
const duration = 9000;
const spacing = 20;
function createSVGObject(link) {
const div = document.createElement(&#39;div&#39;);
div.classList.add(&#39;svg-container&#39;);
const svg = document.createElementNS(&#39;http://www.w3.org/2000/svg&#39;, &#39;svg&#39;);
const text = document.createElementNS(&#39;http://www.w3.org/2000/svg&#39;, &#39;text&#39;);
const a = document.createElement(&#39;a&#39;);
text.setAttribute(&#39;y&#39;, &#39;20&#39;);
text.setAttribute(&#39;font-family&#39;, &#39;Arial, Helvetica, sans-serif&#39;);
text.textContent = link.text;
svg.appendChild(text);
a.appendChild(svg);
div.appendChild(a);
a.setAttribute(&#39;href&#39;, link.href);
a.setAttribute(&#39;class&#39;, &#39;svg&#39;);
const computedWidth = text.getComputedTextLength();
div.style.width = (computedWidth + 10) + &#39;px&#39;;
svg.setAttribute(&#39;width&#39;, (computedWidth + 10) + &#39;px&#39;);
svg.setAttribute(&#39;height&#39;, &#39;30&#39;);
svg.setAttribute(&#39;viewBox&#39;, `0 0 ${computedWidth + 10} 30`);
svg.style.overflow = &#39;visible&#39;;
return div;
}
function animateSVGObject(object) {
object.style.top = &#39;0&#39;;
object.style.left = window.innerWidth + &#39;px&#39;;
const startTime = performance.now();
requestAnimationFrame(function animate(time) {
const progress = (time - startTime) / duration;
const distance = (window.innerWidth + object.getBoundingClientRect().width + spacing) * progress;
const currentPosition = window.innerWidth - distance;
object.style.left = currentPosition + &#39;px&#39;;
if (currentPosition + object.getBoundingClientRect().width &gt; -object.getBoundingClientRect().width) {
requestAnimationFrame(animate);
} else {
setTimeout(() =&gt; {
object.style.left = window.innerWidth + &#39;px&#39;;
animateSVGObject(object);
}, duration * links.length);
}
});
}
links.forEach((link, index) =&gt; {
const svgObject = createSVGObject(link);
document.getElementById(&#39;svg-container&#39;).appendChild(svgObject);
setTimeout(() =&gt; animateSVGObject(svgObject), duration * index);
});
&lt;/script&gt;
&lt;p&gt;SVG&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
&#39;&#39;&#39;

<s>Any takes on what I am doing wrong with the directionand why the svg does not animate all the way across my screen horizontally ? </s>

答案1

得分: 0

以下是您提供的代码的翻译部分:

这是我目前得到的结果

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>具有可点击超链接的SVG动画滚动条</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Einar Petersen - 科幻作家和技术爱好者</title>
  <link rel="stylesheet" type="text/css" href="ticker.css">
  <link rel="stylesheet" type="text/css" href="banner.css">
  <link rel="stylesheet" href="indexthumbnails.css">
  <link rel="stylesheet" type="text/css" href="navigation.css">
  <script src="scripts.js" defer></script>
  <meta name="description" content="... - 此页面包含一个具有可点击超链接的SVG动画滚动条">
  <style>
    body, html {
      margin: 0;
      padding: 0;
      height: 100%;
      width: 100%;
    }
    #svg-container {
      position: relative;
      overflow: hidden;
      height: 100%;
      width: 100%;
      white-space: nowrap;
    }
    .svg-container {
      position: absolute;
      width: auto;
      height: 30px;
    }
    .svg {
      display: inline-block;
      cursor: pointer;
    }
    .svg text {
      fill: #333;
      font-size: 14px;
      font-family: Arial, Helvetica, sans-serif;
    }
  </style>
</head>
<body>
  <div id="svg-container"></div>
  <script>
    const links = [
      { text: '欢迎来到即将全面改版的网站 - 使用AI进行构建 - 单击以了解更多。', href: './aiwork.html' },
    ];

    const duration = 9000;
    const spacing = 20;

    function createSVGObject(link) {
      const div = document.createElement('div');
      div.classList.add('svg-container');

      const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
      const text = document.createElementNS('http://www.w3.org/2000/svg', 'text');
      const a = document.createElement('a');

      text.setAttribute('y', '20');
      text.setAttribute('font-family', 'Arial, Helvetica, sans-serif');
      text.textContent = link.text;

      svg.appendChild(text);
      a.appendChild(svg);
      div.appendChild(a);

      a.setAttribute('href', link.href);
      a.setAttribute('class', 'svg');

      const computedWidth = text.getComputedTextLength();
      div.style.width = (computedWidth + 10) + 'px';
      svg.setAttribute('width', (computedWidth + 10) + 'px');
      svg.setAttribute('height', '30');
      svg.setAttribute('viewBox', `0 0 ${computedWidth + 10} 30`);
      svg.style.overflow = 'visible';

      return div;
    }

    function animateSVGObject(object) {
      object.style.top = '0';
      object.style.transform = `translateX(${window.innerWidth}px)`;

      const startTime = performance.now();
      requestAnimationFrame(function animate(time) {
        const progress = (time - startTime) / duration;
        const distance = (window.innerWidth + object.getBoundingClientRect().width + spacing) * progress;
        const currentPosition = window.innerWidth - distance;
        object.style.transform = `translateX(${currentPosition}px)`;

        if (currentPosition + object.getBoundingClientRect().width > -spacing * 27) {
          requestAnimationFrame(animate);
        } else {
          setTimeout(() => {
            object.style.transform = `translateX(${window.innerWidth}px)`;
            animateSVGObject(object);
          }, duration * links.length);
        }
      });
    }

    links.forEach((link, index) => {
      const svgObject = createSVGObject(link);
      document.getElementById('svg-container').appendChild(svgObject);
      setTimeout(() => animateSVGObject(svgObject), duration * index);
    });
  </script>
  <p>SVG</p>
  <footer>
    <p>© 2023 .... 保留所有权利。</p>
    <p>本网站上的某些材料可能受不同许可的约束,在适用的地方将清楚指出。</p>
    <ul>
      <li><a href="...">... 在Facebook上</a></li>
      <li><a href="...">在Twitter上关注... </a></li>
      <li><a href="#">Instagram</a></li>
      <li><a href="...">成为赞助者</a></li>
    </ul>
  </footer>
</body>
</html>

请注意,上述翻译只包括HTML代码的部分。如果您需要进一步的翻译或有其他问题,请随时告诉我。

英文:

This was what I ended up with for now

   &lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;title&gt;SVG Aninimated Ticker With Clickable Hyperlinks &lt;/title&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;title&gt;Einar Petersen - Science Fiction Writer and Technology Enthusiast&lt;/title&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;ticker.css&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;banner.css&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;indexthumbnails.css&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;navigation.css&quot;&gt;
&lt;script src=&quot;scripts.js&quot; defer&gt;&lt;/script&gt;
&lt;meta name=&quot;description&quot; content=&quot;... - This page in particular contains a SVG Aninimated Ticker With Clickable Hyperlinks&quot;&gt;
&lt;style&gt;
body, html {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
}
#svg-container {
position: relative;
overflow: hidden;
height: 100%;
width: 100%;
white-space: nowrap;
}
.svg-container {
position: absolute;
width: auto;
height: 30px;
}
.svg {
display: inline-block;
cursor: pointer;
}
.svg text {
fill: #333;
font-size: 14px;
font-family: Arial, Helvetica, sans-serif;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;svg-container&quot;&gt;&lt;/div&gt;
&lt;script&gt;
const links = [
{ text: &#39;Welcome to a soon fully revamped site - using AI to construct it - click to learn more.&#39;, href: &#39;./aiwork.html&#39; },
];
const duration = 9000;
const spacing = 20;
function createSVGObject(link) {
const div = document.createElement(&#39;div&#39;);
div.classList.add(&#39;svg-container&#39;);
const svg = document.createElementNS(&#39;http://www.w3.org/2000/svg&#39;, &#39;svg&#39;);
const text = document.createElementNS(&#39;http://www.w3.org/2000/svg&#39;, &#39;text&#39;);
const a = document.createElement(&#39;a&#39;);
text.setAttribute(&#39;y&#39;, &#39;20&#39;);
text.setAttribute(&#39;font-family&#39;, &#39;Arial, Helvetica, sans-serif&#39;);
text.textContent = link.text;
svg.appendChild(text);
a.appendChild(svg);
div.appendChild(a);
a.setAttribute(&#39;href&#39;, link.href);
a.setAttribute(&#39;class&#39;, &#39;svg&#39;);
const computedWidth = text.getComputedTextLength();
div.style.width = (computedWidth + 10) + &#39;px&#39;;
svg.setAttribute(&#39;width&#39;, (computedWidth + 10) + &#39;px&#39;);
svg.setAttribute(&#39;height&#39;, &#39;30&#39;);
svg.setAttribute(&#39;viewBox&#39;, `0 0 ${computedWidth + 10} 30`);
svg.style.overflow = &#39;visible&#39;;
return div;
}
function animateSVGObject(object) {
object.style.top = &#39;0&#39;;
object.style.transform = `translateX(${window.innerWidth}px)`;
const startTime = performance.now();
requestAnimationFrame(function animate(time) {
const progress = (time - startTime) / duration;
const distance = (window.innerWidth + object.getBoundingClientRect().width + spacing) * progress;
const currentPosition = window.innerWidth - distance;
object.style.transform = `translateX(${currentPosition}px)`;
if (currentPosition + object.getBoundingClientRect().width &gt; -spacing * 27) {
requestAnimationFrame(animate);
} else {
setTimeout(() =&gt; {
object.style.transform = `translateX(${window.innerWidth}px)`;
animateSVGObject(object);
}, duration * links.length);
}
});
}
links.forEach((link, index) =&gt; {
const svgObject = createSVGObject(link);
document.getElementById(&#39;svg-container&#39;).appendChild(svgObject);
setTimeout(() =&gt; animateSVGObject(svgObject), duration * index);
});
&lt;/script&gt;
&lt;p&gt;SVG&lt;/p&gt;
&lt;footer&gt;
&lt;p&gt;&#169; 2023 .... All rights reserved.&lt;/p&gt;
&lt;p&gt;Some material on this website may be subject to different licenses, which will be clearly indicated where applicable.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;...&quot;&gt;... on Facebook&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;...&quot;&gt;Follow ... @ Twitter&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Instagram&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;...&quot;&gt;Become a patron&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/footer&gt;
&lt;/body&gt;
&lt;/html&gt;

huangapple
  • 本文由 发表于 2023年4月10日 21:34:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977588.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定