更改Spring Boot中JavaScript的图像源。

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

Change image source from JavaScript in spring boot

问题

<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<body>

<div style="text-align:center">
    <button onclick="playPause()">Play/Pause</button>
    <button onclick="zoomOut()">Zoom Out</button>
    <button onclick="zoomIn()">Zoom In</button>
    <br><br>
    <img id="img1" src="../static/images/6car_1.png"; th:src="@{images/6car_1.png}"/>
</div>

<script>
    var zoom = 6;
    var initialPath = "../static/images/";
    var count = 1;
    var isPlaying = true;
    playPhotos();
    function playPause() {
        if (isPlaying)
            isPlaying = false;
        else{
            isPlaying = true;
            playPhotos();
        }
    }
    function playPhotos(){
        var firstPart = initialPath + zoom + "car_" + count + ".png";
        var secondPart = "@{images/" + zoom + "car_" + count + ".png}";
        if(count <= 13){
            document.getElementById("img1").src = firstPart;
            th:src = secondPart;
            count++;
        }
        else{
            count = 1;
            document.getElementById("img1").src = firstPart;
            th:src = secondPart;
        }
        if(isPlaying){
            setTimeout("playPhotos()", 200);
        }
    }
    function zoomOut(){
        if((zoom - 1) >= 1){
            zoom--;
        }
    }
    function zoomIn(){
        if((zoom + 1) <= 10){
            zoom++;
        }
    }
</script>

</body>
</html>
英文:

I am trying to change the source of an image in an html document every few seconds with a JavaScript function within a spring boot project but the typical way of changing the source does not appear to be working. I have tried to match the format of setting the image source with no luck either.
Here is where I initially set the source:

&lt;!DOCTYPE html&gt;
&lt;html xmlns:th=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;body&gt;
&lt;div style=&quot;text-align:center&quot;&gt;
&lt;button onclick=&quot;playPause()&quot;&gt;Play/Pause&lt;/button&gt;
&lt;button onclick=&quot;zoomOut()&quot;&gt;Zoom Out&lt;/button&gt;
&lt;button onclick=&quot;zoomIn()&quot;&gt;Zoom In&lt;/button&gt;
&lt;br&gt;&lt;br&gt;
&lt;img id = &quot;img1&quot; src=&quot;../static/images/6car_1.png&quot;; th:src=&quot;@{images/6car_1.png}&quot;/&gt;
&lt;/div&gt;
&lt;script&gt;
var zoom = 6;
var initialPath = &quot;../static/images/&quot;;
var count = 1;
var isPlaying = true;
playPhotos();
function playPause() {
if (isPlaying)
isPlaying = false;
else{
isPlaying = true;
playPhotos();
}
}
function playPhotos(){
var firstPart = initialPath  + zoom + &quot;car_&quot; + count +&quot;.png&quot;
var secondPart = &quot;@{images/&quot; + zoom + &quot;car_&quot; + count +&quot;.png}&quot;
if(count &lt;=13){
document.getElementById(&quot;img1&quot;).src = firstPart;
th:src = secondPart;
count++;
}
else{
count = 1;
document.getElementById(&quot;img1&quot;).src = firstPart;
th:src = secondPart;
}
if(isPlaying){
setTimeout(&quot;playPhotos()&quot;,200);
}
}
function zoomOut(){
if((zoom - 1) &gt;= 1){
zoom--;
}
}
function zoomIn(){
if((zoom + 1) &lt;= 10){
zoom++;
}
}
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

答案1

得分: 0

我编辑了它,它能够工作,因为我创建了变量"zoom"、"isPlaying"并调用了playPhotos()函数。你没有指定这些细节吗?如果除了函数操作之外还有错误,请将您的所有代码作为代码片段上传。我会相应地更新我的答案。

var initialPath = "../static/images/";
var count = 1;
var isPlaying = true;
let zoom = "zoom";
playPhotos()

function playPhotos() {
  var firstPart = `${initialPath}${zoom}car_${count}.png`; // 尝试
  var secondPart = `@{images/${zoom}car_${count}.png`;  // 尝试
  if (count <= 13) {
    document.getElementById("img1").src = firstPart;
    th: src = secondPart;
    count++;
  } else {
    count = 1;
    document.getElementById("img1").src = firstPart;
    th: src = secondPart;
    count++;
  }
  if (isPlaying) {
    setTimeout("playPhotos()", 200);
  }
}
<img id="img1" src="../static/images/6car_1.png" ; th:src="@{images/6car_1.png}" />
英文:

I edited it and it works because I created the variable "zoom" , "isPlaying" and called playPhotos (). You didn't specify those details ? If you have an error other than function operation, please upload all of your code as a snippet. I'll update my answer accordingly.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var initialPath = &quot;../static/images/&quot;;
var count = 1;
var isPlaying = true;
let zoom = &quot;zoom&quot;
playPhotos()
function playPhotos() {
var firstPart = `${initialPath}${zoom}car_${count}.png`; //try
var secondPart = `@{images/${zoom}car_${count}.png`;  // try
if (count &lt;= 13) {
document.getElementById(&quot;img1&quot;).src = firstPart;
th: src = secondPart;
count++;
} else {
count = 1;
document.getElementById(&quot;img1&quot;).src = firstPart;
th: src = secondPart;
count++;
}
if (isPlaying) {
setTimeout(&quot;playPhotos()&quot;, 200);
}
}

<!-- language: lang-html -->

&lt;img id=&quot;img1&quot; src=&quot;../static/images/6car_1.png&quot; ; th:src=&quot;@{images/6car_1.png}&quot; /&gt;

<!-- end snippet -->

答案2

得分: 0

var secondPart = "/images/" + zoom + "car_" + count + ".png";
document.getElementById("img1").src = secondPart;
英文:
var secondPart = &quot;/images/&quot;+zoom+&quot;car_&quot;+count+&quot;.png&quot;;
document.getElementById(&quot;img1&quot;).src = secondPart;

huangapple
  • 本文由 发表于 2020年5月31日 06:02:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/62109199.html
匿名

发表评论

匿名网友

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

确定