简体   繁体   English

如何更改 flexslider 中的图像?

[英]How to change images in flexslider?

I know this is a very basic question but it has me very confused - there's not much documentation that I could find about the flexslider.我知道这是一个非常基本的问题,但它让我很困惑——我找不到太多关于 flexslider 的文档。 Also, I'm very new to this language, I'm a C++ / ASM type person but trying to get a webpage setup.另外,我对这种语言很陌生,我是 C++ / ASM 类型的人,但正在尝试设置网页。 The page is for an inte.net radio station.该页面适用于 inte.net 广播电台。 The slider is used to show the album covers of the currently playing, next, and later albums. slider 用于显示当前播放、下一张和以后专辑的专辑封面。 The image names are 'hard-coded' in the slider which is fine if the images don't need to change.图像名称在 slider 中是“硬编码”的,如果不需要更改图像,这很好。 The problem is that the images do change every 3 minutes but the browser caches them so even if the image file contents change, the old image data displays.问题是图像每 3 分钟更改一次,但浏览器会缓存它们,因此即使图像文件内容更改,也会显示旧图像数据。

I spent the last few weeks developing a Windows service that updates the actual image files (Playing.jpg, Next.jpg, etc.) from the SQL database (among other things) only to find out once the slider is initialized, the displayed images don't change.我在过去几周开发了一项 Windows 服务,该服务从 SQL 数据库(除其他外)更新实际图像文件(Playing.jpg、Next.jpg 等),结果发现一旦 slider 被初始化,显示的图像不要改变。

Anyway, is the slider just used for 'static' images?无论如何,slider 是否仅用于“静态”图像? Any advice as to how to update the images dynamically?关于如何动态更新图像的任何建议? php / js? php / JS?

<li> <img src="Playing.jpg" alt="" >
   <div class="flex-caption">
      <h2>Now Playing</h2>
   </div>
</li>

Thank you!谢谢!

Keeping the filenames the same but changing the image contents makes no difference.保持文件名相同但更改图像内容没有区别。 What's the general way of changing slider images dynamically?动态更改 slider 图像的一般方法是什么? Code snippets would be appreciated.代码片段将不胜感激。

To avoid the browser cache you can add a random query like that:为了避免浏览器缓存,您可以添加这样的随机查询:

PHP: PHP:

<li> <img src="Playing.jpg?v=<?php echo rand(); ?>" alt="" >
   <div class="flex-caption">
      <h2>Now Playing</h2>
   </div>
</li>

JS:记者:

<li> <img src="Playing.jpg" alt="" id="imageid">
   <div class="flex-caption">
      <h2>Now Playing</h2>
   </div>
</li>
<script>
document.getElementById("imageid").src="Playing.jpg?v="+Math.random();
</script>

Mehedi's answer is good but random strings can collide and Math.random() is returning a float, an integer would be better. Mehedi 的回答很好,但随机字符串可能会发生冲突,并且Math.random()返回一个浮点数,integer 会更好。
So, it is better to use server current UNIX time eg:所以,最好使用服务器当前 UNIX 时间,例如:

PHP PHP

<li>
  <img src="Playing.jpg?v=<?php echo time(); ?>" alt="" />
  <div class="flex-caption">
    <h2>Now Playing</h2>
  </div>
</li>

JavaScript JavaScript

 document.getElementById("imageid").src = `Playing.jpg?v=${Date.now()}`; document.getElementById("imageid").alt = `Image Name: Playing.jpg?v=${Date.now()}`;
 <li> <img src="Playing.jpg" alt="" id="imageid" /> <div class="flex-caption"> <h2>Now Playing</h2> </div> </li>

However , this is bad for the user as he is always downloading images.然而,这对用户不利,因为他总是在下载图像。 The best solution would be to change the filenames from database and pass that data somehow to your code (maybe an API endpoint or something).最好的解决方案是更改数据库中的文件名并将该数据以某种方式传递给您的代码(可能是 API 端点或其他东西)。
Also, since images are changing every 3 minutes, you could set a cookie in localStorage and check with it for intervals of 3 minutes before passing another time in your <img> .此外,由于图像每 3 分钟更改一次,您可以在localStorage中设置一个 cookie,并每隔 3 分钟检查一次,然后再在<img>中传递一次。

Personally, I would go with an API-approach.就个人而言,我会使用 API 方法 go。

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

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