Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5954443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:58:18+00:00 2026-05-22T17:58:18+00:00

I use image sprites for my gallery. To get the right background image in

  • 0

I use image sprites for my gallery. To get the right background image in the right place I use this:

<style>
#container div:nth-child(1){background-position:0 0}
#container div:nth-child(2){background-position:200px 0}
#container div:nth-child(3){background-position:400px 0}
#container div:nth-child(4){background-position:600px 0}
#container div:nth-child(5){background-position:800px 0}
#container div:nth-child(6){background-position:1000px 0}
</style>

It’s kind of time consuming to count all background values when there are many div tags and when the number to add is for example 73px instead of 200. Also it takes up lots of css code.

Can I achieve the same result in another way using CSS3?

Otherwise I wonder if someone could have a look on my jquery script, to see if there is something that can be done in a better way (the script works):

<div id="container">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
</div>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script>
$(function() {
$("#container div:first-child").fadeIn(400, showNext);
});

function showNext() {
var test1 = $(this).index(),
test2 = test1*200;

if (navigator.appName=='Microsoft Internet Explorer'){
$(this).css('backgroundPositionX', -test2);
}
else{
$(this).css('backgroundPosition', -test2);
}

$(this).next().fadeIn(400, showNext);
}
</script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:58:19+00:00Added an answer on May 22, 2026 at 5:58 pm

    I didn’t have a background image to work off of, but here’s an example of using a function passed to CSS to modify the height of an element. What you want to do is just swap out the height property with your backgroundPosition one, and switch the return value to be negative.

    This should substantially minimize your code.

    $(function() {
        $("#container div").css('height', function(i, oldValue) {
            return (i+1)*200;
        }).fadeIn(400);
    });
    

    The proof is in the fiddle.

    If you’re looking to modify more than one CSS property, you’ll want to use map or some of the other methods outlined on the .CSS example page: http://api.jquery.com/css/

    Oh, and as a side note, never explicitly detect a browser like that. You want to do feature detection, since I bet your code breaks IE9, assuming IE9 handles background positioning differently now than IE8. http://www.jibbering.com/faq/notes/detect-browser/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to use CSS sprites for the list background image? Normally, I
I want to use CSS sprites on a web site instead of separate image
I trying to use ImageInfo and Jython to get information from a image on
How do you approach the use of image sprites in css? Should I take
I'm trying to use background image in CSS but even though I gave the
I want to use an image or icon as a custom cursor in WPF
Is it possible to use an image captured with the iPhone's camera as a
Is SQL Server 2008 a good option to use as an image store for
I am trying to use the boost generic image library to convert CYMK images
How can i use Tomcat to serve image files from a public folder outside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.