Hi there, I'm dynamically pulling my sidebar image from a database, the image is resized to fit based on which dimension is smaller. Then I'm using a jquery script to reposition the image so that any overflow on the width overflows both sides rather than just on the right using this script:

<script type="text/javascript">


$(document).ready(function()
{
   $("#sidebar-image>img").each(function(i, img) {
        $(img).css({
            position: "relative",
            left: ($(img).parent().width() - $(img).width()) / 2
        });
    });
});
</script>

The problem I'm having is that sometimes the width isn't getting calculated properly, it doesn't seem to get the width of the image everytime and I'm assuming that maybe the jquery is firing before the image is pulled from the database? If I refresh the page the image positions properly.

Have you tried getting the image dimension using PHP, then passing the information into JQuery?

JQuery shouldn't be firing before the php (Client/Server)

Member Avatar for LastMitch

@dyingatmidnight

The problem I'm having is that sometimes the width isn't getting calculated properly, it doesn't seem to get the width of the image everytime and I'm assuming that maybe the jquery is firing before the image is pulled from the database? If I refresh the page the image positions properly.

So far the code you provided doesn't show any PHP & MYSQL code. It's only in JQuery? My question is how can anyone help you if you don't put the other code with the JQuery!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.