Results 1 to 7 of 7

Thread: javascript controlling images

  1. #1
    Join Date
    Nov 2002

    javascript controlling images

    I've tried this in the graphics forum without much luck, so thought I'd throw it to the java monkies to see what can be done (no offence intended!):

    I'm using a java script image gallery thing that scrolls through some images on my page. Nice and simple, works well, and I'm not too bothered about the non javascript views who cant see it since it's hardly a matter of life and death, and if they dont want to use the bloomin' technlology that someones taken the time to invent then that's up to them.


    anyway, the problem is that as it scrolls through the images, I would love it if I had some way of checking what width the image that's about to be displayed is. Then, if it saw that the image was over 360, for example, it set the width of the image to 360 so that it got scaled down. i dont want to have to tell it the dimensions of every image using the width/height commands, and nor do i want EVERY image to display at 360 width - if it's slimmer than that then I'm quite happy for it to stay that way. It's just for the ones that are bigger than 360 that will mess up my tables, they're the ones that I want scaled down so that they fit.

    Am I being far too optimistic with this, would I need an applet, or is it just not possible at all?

    help me java-script guru's, you're my only hope...

  2. #2
    Join Date
    Nov 2002
    It's do-able, but without seeing your code I can't really help much. But what I would do, instead of checking the images as it is coming into focus, is just loop through them all at the start, and check/set their size then.
    Last edited by Rick Bull; 11-26-2002 at 07:47 AM.

  3. #3
    Join Date
    Nov 2002
    The java I'm using preloads the images so that it can scroll through them when the user clicks on the "next image" button. Is there code that i can put into an if cycle along the lines of;
    If image.width>360 then

    or something like that?

  4. #4
    Join Date
    Nov 2002
    Something like this should work:

    if (image.width>360)
    (you don't need the else bit though, as it's not changing any values). Again if you need more specific code you'll have to provide the HTML/JavaScript you already have.

  5. #5
    Join Date
    Nov 2002
    Ah so I wasn't too far off then! Where would I put that into my code? I cant put it in with the img handles since the dimensions for each image are different. And can I use image.width even though I havent defined it as a variable? Is it just recognised by java? Do I put it in with each images entry in the preloading list? And actually surely I cant use
    a '>' sign since the HTML will think that I'm trying to put some markup in?
    Last edited by jpmoriarty; 11-26-2002 at 10:01 AM.

  6. #6
    Join Date
    Nov 2002
    That's a lot of questions You should be able to use image.width even if you haven't set the attribuets according to DOM 2 at least. You can use >, according to the HTML 4.01 specs, you can use > (as long as you have comments around the <script> elements) but not </, you need to escape them, e.g.

    <script type="text/javascript"><!--
    And as for referring to the images there are several ways to do it. Probably the way I would do it would be to give all your img tags an ID, starting with a string like "thumb_", then loop through all images on the page, if they have that at the start then set the width. I'll try to put together a script for you.

  7. #7
    Join Date
    Nov 2002
    OK, try something like this:

    <script type="text/javascript"><!--
      function setImageSize() {
        var thumbPrefix = "thumb_"; //What's before thumbnail's IDs
        var maxWidth = 360, maxHeight = 360; //Max size of thumbnails
        //If we can access the images
        if (document.images) {
          //Loop for all images
          for (var loopCounter = 0; loopCounter < document.images.length; loopCounter++) {
            //If this one starts with thumbPrefix
            if (document.images[loopCounter].id.substring(0, thumbPrefix.length) == thumbPrefix) {
              //If this one is too wide, shrink it
              if (document.images[loopCounter].width > maxWidth)
                document.images[loopCounter].width = maxWidth;
              //Same for height
              if (document.images[loopCounter].height > maxHeight)
                document.images[loopCounter].height = maxHeight;
    <body onload="setImageSize();">
        <img id="thumb_1" src="thumb_one.png" alt="" />
        <img id="thumb_2" src="thumb_2.png" alt="" />
        <img id="thumb_3" src="thumb_3.png" alt="" />
        <img id="thumb_4" src="thumb_four.png" alt="" />
    You will have to set an (invidual) ID on each image starting with "thumb_". You can also change it to check the src attribute, just that I had that before, but the full filename gets returned, so it doesn't work, but it maybe OK online.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.17415 seconds
  • Memory Usage 2,903KB
  • Queries Executed 13 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (3)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (7)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • bbcode_parse_start
  • postbit_imicons
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates