www.webdeveloper.com
Results 1 to 12 of 12

Thread: Getting a random picture to display

  1. #1
    Join Date
    Mar 2012
    Posts
    20

    Getting a random picture to display

    Hi, I'm trying to get it so that a random picture displays from a gallery file one at a time. I'm probably making a basic syntax error somewhere as I'm fairly new to JavaScript/jQuery, but please can someone help? I can't get a picture to display in the div at all. Any help would be great, thanks!!

    N.B. the pictures are called e.g. 101.jpg, 102.jpg - so I thought a random number generator would be best to get a random picture.

    <script>

    $(document).ready(function generatePic(){

    function randomPicture() {

    var min=101;
    var max=120;

    var picture=Math.floor(Math.random()*(max-min+1))+min;

    }

    function bannerDisplay(){
    $("#banner").fadeOut('400',function(){
    $("#banner").load("pages/gallery/PhotoWall/images/'$picture'.jpg", function(){
    $("#banner").fadeIn('600', randomPicture())
    })})}

    bannerDisplay();

    }
    )

    </script>

    <div id="banner"></div>

  2. #2
    Join Date
    Mar 2012
    Posts
    20
    Sorry - the code is:

    <script>

    $(document).ready(function generatePic(){

    function randomPicture() {

    var min=101;
    var max=120;

    var picture=Math.floor(Math.random()*(max-min+1))+min;

    }

    function bannerDisplay(){
    $("#banner").fadeOut('400',function(){
    $("#banner").load("pages/gallery/PhotoWall/images/'$picture'.jpg", function(){
    $("#banner").fadeIn('600')
    })})}
    })

    generatePic();

    </script>

    <div id="banner"></div>

  3. #3
    Join Date
    Mar 2012
    Posts
    20
    I've been playing around with this a bit, and have tried using arrays - now I can get something to appear in the div, but it looks like this:
    �V��$��F��Ⱦ36�H�W���/+�N�FC�C b8͔��@�5^��
    etc... so I assume it is pulling it up in the wrong format or something? Also, the image isn't changing, it only changes when I refresh the page - am I not calling the function back correctly?
    Any help on how to correct this would be much appreciated, thanks!!

    New code:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    	<title></title>
    	<script>
    
    function randomImage(){
    var images=new Array()
    
    images[1]="images/101.jpg"
    images[2]="images/102.jpg"
    images[3]="images/103.jpg"
    images[4]="images/104.jpg"
    images[5]="images/105.jpg"
    images[6]="images/106.jpg"
    images[7]="images/107.jpg"
    images[8]="images/108.jpg"
    images[9]="images/109.jpg"
    images[10]="images/110.jpg"
    images[11]="images/111.jpg"
    images[12]="images/112.jpg"
    images[13]="images/113.jpg"
    images[14]="images/114.jpg"
    images[15]="images/115.jpg"
    images[16]="images/116.jpg"
    images[17]="images/117.jpg"
    images[18]="images/118.jpg"
    images[19]="images/119.jpg"
    images[20]="images/120.jpg"
    
    
    var ranPic=Math.floor(Math.random()*images.length)
    
    			$("#words").fadeOut('300',function(){
    			$("#words").load(images[ranPic], function(){
    			$("#words").fadeIn('400');
    			})})
    }
    
    
    randomImage();
    
    </script>
    </head>
    
    <body>
    
    <div id="words"></div>
    </body>
    </html>

  4. #4
    Join Date
    Feb 2012
    Posts
    218
    First of all, use tabs in your code
    Code:
    <script>
    $(document).ready(function(){
    	
    	function randomPicture() {
    		var min=101;
    		var max=120;
    		var picture=Math.floor(Math.random()*(max-min+1))+min;
    		return picture;
    	}
    
    	function bannerDisplay(){
    		$("#banner").fadeOut('400',function(){
    			$("#banner").load("pages/gallery/PhotoWall/images/"+randomPicture()+".jpg", function(){
    				$("#banner").fadeIn('600')
    			})
    		})
    	}
    	
    	bannerDisplay();
    })
    
    </script>
    <div id="banner"></div>
    Haven't tested it, but you should make an ideea

  5. #5
    Join Date
    Mar 2012
    Posts
    20
    Hi,

    Thanks for that - sorry about tabs, think it copied and pasted funny from my editor.

    I have tried using that code, and it appears to be pulling something into the div - however, all that appears is lots of garble like this:

    3r��$�%CS������+!1QA"2Raq�#3B�� 

    So I am assuming it is interpreting the image as a string or something? How can I get just the image to appear?

    Thanks!!

  6. #6
    Join Date
    Feb 2012
    Posts
    218
    Yes, sorry, as I said, I haven't tested it. Here is an working code
    Code:
    	function bannerDisplay(){
    		$("#banner").fadeOut('400',function(){
    			var img = new Image()
    			var imageSrc = "pages/gallery/PhotoWall/images/"+randomPicture()+".jpg";
    			$(img).load(function(){
    				$("#banner").html('').append($(this)).fadeIn('600');
    			}).attr('src', imageSrc);
    		})
    	}

  7. #7
    Join Date
    Mar 2012
    Posts
    20
    Thanks so much - sorry I'm fairly new at all of this so I don't know how to do all of that!

    Got this now - but problem is that now nothing displays in the div at all - if I remove the bannerGen function completely then a picture does display in the div, but I need the picture to change, about every 4 seconds - how do I do this!? Use a timeout or a pause or something? I need to be able to get the bannerGen function to loop indefinately.

    Thanks for your help!


    Code:
        <script>
        $(document).ready(function(){
    
            function bannerGen() {
    
            function randomPicture() {
                var min=101;
                var max=120;
                var picture=Math.floor(Math.random()*(max-min+1))+min;
                return picture;
            }
    
            function bannerDisplay(){
                $("#banner").fadeOut('400',function(){
                    var img = new Image()
                    var imageSrc = "pages/gallery/PhotoWall/images/"+randomPicture()+".jpg";
                    $(img).load(function(){
                        $("#banner").html('').append($(this)).fadeIn('600');
                    }).attr('src', imageSrc);
                })
            }
            }
            bannerGen();
        )})
    
        </script>
        <div id="banner"></div>
    Last edited by emtw; 03-12-2012 at 08:47 AM.

  8. #8
    Join Date
    Feb 2012
    Posts
    218
    Pay attention to my previous post http://www.webdeveloper.com/forum/sh...41&postcount=4
    Look at the the red lines.

  9. #9
    Join Date
    Mar 2012
    Posts
    20
    That code does get a random image to appear where I want it to - but I need the image to refresh every 4 seconds or so and I don't know how to get the whole function to cycle after this time frame. I'm fairly new to JS/jQ, so I apologise if this is a fairly noob question - I don't fully understand the code you have written enough to edit it myself. Thank you :0)

  10. #10
    Join Date
    Feb 2012
    Posts
    218
    Ok, here is the complete code
    Code:
    $(document).ready(function(){
    	
    	function randomPicture() {
    		var min=101;
    		var max=120;
    		var picture=Math.floor(Math.random()*(max-min+1))+min;
    		return picture;
    	}
    
    	function bannerDisplay(){
    		$("#banner").fadeOut('400',function(){
    			var img = new Image()
    			var imageSrc = "pages/gallery/PhotoWall/images/"+randomPicture()+".jpg";
    			var imageSrc = "auto2moto.png";
    			$(img).load(function(){
    				$("#banner").html('').append($(this)).fadeIn('600');
    			}).attr('src', imageSrc);
    		})
    	}
    	
    	bannerDisplay();
    	window.setInterval(bannerDisplay, 2000);
    })
    2000 means 2 seconds. Set whatever value you want

  11. #11
    Join Date
    Mar 2012
    Posts
    20
    Thanks so much for your help!! It's working great now, thanks :0)

    - although - this line somehow found its way in there:
    var imageSrc = "auto2moto.png";

    but once that was removed it worked great lol!

  12. #12
    Join Date
    Feb 2012
    Posts
    218
    Yes, sorry, that was mine. Instead of creating directory "pages/gallery/PhotoWall/images/ I put a fake image, so I can see it.
    Glad it works as expected.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.48256 seconds
  • Memory Usage 2,975KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)SHOWTHREAD
  • (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
  • (5)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (12)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (12)postbit
  • (12)postbit_onlinestatus
  • (12)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 (73):
  • 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_postinfo_query
  • fetch_postinfo
  • 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
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • 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