带有一个精灵的 Javascript 照片幻灯片

发布于 2024-10-04 06:14:32 字数 1069 浏览 1 评论 0原文

我刚刚为幻灯片编写了一段 js 代码,仅使用一张图像(精灵)。使用 js,我正在更改背景位置以更改可见图像。

var first = true;
function galesprite() {
  if (first) {
    var bg_pos = '0px 0px';
    first = false;
  } else {
    var bg_pos = $("#gmap").css('background-position');
  }

  /* Sub función para cuando termine la animación de fadeOut, sinó la foto se 
   * cambia antes y no crea el efecto deseado. */
  $("#gmap").fadeOut(1000, function() {
    var xy = bg_pos.split(' ');
    var y = xy[1].split('px');
    var newy = parseInt(y) + 200;
    var bgpos = xy[0] + ' ' + newy + 'px';
    $('#gmap').css('background-position', bgpos);
    $("#gmap").fadeIn(1000);
    setTimeout('galesprite()', 4000);
  });
}

受影响的html是这样的。使用一个放置背景图像的 css 类,一组垂直图像。每个图像的高度为 200 像素。

<div id="gmap"></div>

#gmap {
    height: 200px;
    width: 270px;
    background-image: url('../images/diapositiva-home/surf.jpg');
    background-position: 0px 0px;
}

我所做的是定期循环增加 div 的背景 Y 位置。所以看起来这改变了形象。 这个想法是使用精灵来最小化页面的加载时间,而不是加载 10 个图像。 你认为我有什么可以改进的地方吗?谢谢。

I just wrote a js code for a slideshow, using only one image (sprite). With js I'm changing the background position to change the visible image.

var first = true;
function galesprite() {
  if (first) {
    var bg_pos = '0px 0px';
    first = false;
  } else {
    var bg_pos = $("#gmap").css('background-position');
  }

  /* Sub función para cuando termine la animación de fadeOut, sinó la foto se 
   * cambia antes y no crea el efecto deseado. */
  $("#gmap").fadeOut(1000, function() {
    var xy = bg_pos.split(' ');
    var y = xy[1].split('px');
    var newy = parseInt(y) + 200;
    var bgpos = xy[0] + ' ' + newy + 'px';
    $('#gmap').css('background-position', bgpos);
    $("#gmap").fadeIn(1000);
    setTimeout('galesprite()', 4000);
  });
}

And the html affected is this. With a css class that puts the background image, a set of vertical images. Each image has 200px height.

<div id="gmap"></div>

#gmap {
    height: 200px;
    width: 270px;
    background-image: url('../images/diapositiva-home/surf.jpg');
    background-position: 0px 0px;
}

What I do is loop periodicaly increasing the background Y position for the div. So it seems that changes the image.
The idea is minimize the loading time of the page using the sprite instead of loading 10 images.
Do you see anything I could improve? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你列表最软的妹 2024-10-11 06:14:32

精灵最适合小图标 - 而不是大画廊。

如果您有 600kb 的精灵,那么在整个内容下载完成之前您将无法显示第一张图像(例外)。

我通常会避免使用大型 JPEG 精灵,而只需执行...

var img = new Image();

img.onload = function() {
   // Loaded, we can now show the next image
}

img.src = 'bob.jpg'; 

这意味着您可以在显示当前图像的同时下载下一张图像,并在知道它已完成下载时切换到它。

Sprites are best for small icons - not large galleries.

If you have a 600kb sprite, then you won't be able to show the first image until the whole thing has downloaded (with exception).

I generally avoid sprites for large JPEGs, and simply do a...

var img = new Image();

img.onload = function() {
   // Loaded, we can now show the next image
}

img.src = 'bob.jpg'; 

This means you can download the next image whilst showing the current one, and switch to it when you know it has finished downloading.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文