简体   繁体   English

使用jquery的CSS Sprites

[英]CSS Sprites with jquery

I would like to control a css sprite with javascript/jquery. 我想用javascript / jquery控制一个css sprite。 Based on a user input the image should move position. 根据用户输入,图像应移动位置。

The case: I have an image container with 400x400, but the image I want to show is 400x1200, so I can only display 400 in height at the time, but the width would be constant. 案例:我有一个400x400的图像容器,但我要显示的图像是400x1200,所以我当时只能显示400的高度,但宽度是恒定的。

I would to like to display parts of the image when a user clicks a button. 我想在用户点击按钮时显示部分图像。 I have drawn an example to show what I want: http://0o2471.net/55070 我举了一个例子来展示我想要的东西:http: //0o2471.net/55070

In that mockup you have the image container which is 400x400 marked with black frame, the green background is the "image" that I want to display based on the user input. 在该模型中,您拥有标有黑框的400x400图像容器,绿色背景是我想根据用户输入显示的“图像”。 So let's say the user press the "Button bottom" then the bottom part would change its position to the center(image container.) 因此,假设用户按下“按钮底部”,然后底部将其位置更改为中心(图像容器)。

Any suggestions on how I do this? 有关我如何做的任何建议? Which functions in jquery/javascript should I use? 我应该使用jquery / javascript中的哪些函数? Is css sprites the best way? css精灵是最好的方式吗? I don't need the whole code, just a few directions. 我不需要整个代码,只需几个方向。

Use background-position CSS rule. 使用背景位置CSS规则。 JQuery code for this 这个JQuery代码

$(element).css({'background-position' : '0 -100px'})

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

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