Changing image width using JavaScript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Doug van Vianen

    Changing image width using JavaScript

    Hi,

    I have the following coding on a web page. It causes two pictures (pic1.jpg
    and pic2.jpg)
    to show, one above the other and then when one clicks on the top picture is
    squeezes to
    the left (as its width is reduced) to show the bottom picture. Then when the
    bottom
    picture is clicked the top picture expands to the right to cover the bottom
    picture again.
    Much of the coding is simply to show the pictures a suitable size for the
    user's screen
    size.


    <script language="JavaS cript">
    <!--
    var scnwid=screen.w idth;
    var scnhei=screen.h eight;
    var ii=0;
    var maxii=20;
    var widthf=0;
    var widthw=0;
    var winc=0;
    var topd=10;
    var leftd=0;
    var heightf=350;
    var msgtop=0;
    function Init() {
    // set widths and heights of pictures and divs to match screen size
    widthf=0.875*sc nwid;
    heightf=.6*scnh ei;
    leftd=(scnwid-widthf)/2;
    widthw=widthf;
    document.pic1.w idth = widthf;
    document.pic1.h eight = heightf;
    var ref=document.ge tElementById("d iv1");
    ref.style.top=t opd;
    ref.style.left= leftd;
    ref.style.width =widthf;
    ref.style.heigh t=heightf;
    document.pic2.w idth = widthf;
    document.pic2.h eight = heightf;
    var ref=document.ge tElementById("d iv2");
    ref.style.top=t opd;
    ref.style.left= leftd;
    ref.style.width =widthf;
    ref.style.heigh t=heightf;
    msgtop=heightf+ topd;
    var ref3=document.g etElementById(" div3");
    ref3.style.top= msgtop;
    ref3.style.left =leftd;
    }

    function ChgSize(updown) {
    winc=widthf/maxii;
    winc=updown*win c;
    ii=0;
    DoPics();
    }

    function DoPics() {
    widthw=widthw-winc;
    document.pic1.w idth = widthw;
    ii=ii+1;
    if (ii<maxii) {
    setTimeout("DoP ics()",30);
    }
    }

    -->
    </script>
    </head>

    <body onLoad="Init(); return true">

    <div id=div1
    style="position :absolute;left: 10;top:10;width :700;height:350 ;z-index:1;">
    <a href="JavaScrip t:void(0)" onClick="ChgSiz e(1); return true"><img
    name="pic1" border=0 src="pic1.jpg" width=700 height=400></a>
    </div>

    <div id=div2
    style="position :absolute;left: 10;top:10;width :700;height:350 ;z-index:0;">
    <a href="JavaScrip t:void(0)" onClick="ChgSiz e(-1); return true"><img
    name="pic2" border=0 src="pic2.JPG" width=700 height=400></a>
    </div>

    <p>
    <center>
    <div id=div3 style="position :absolute;left: 10;top:10;z-index:0;">
    Click on the picture to see the other picture.
    </div>
    </center>

    The coding and web page work fine when I display it using the web page
    editor I wrote in
    Microsoft Visual Basic 6 but not when I view the page using Internet
    Explorer. When I do
    the latter there are often, but not always, white flashes that appear as the
    one picture
    width is reduced or expanded. It acts as if the picture with the new width
    is not fully
    displayed before it is time to reduce it by the next increment.

    I am surprised by this since I thought that the browser window in my web
    page editor used
    the same coding as Internet Explorer.

    I have tried to preload the pictures but this did not make any difference.

    Can someone help me with this problem? Is there a better way to do the same
    thing?

    Thank you.
    Doug vV


  • Good Man

    #2
    Re: Changing image width using JavaScript

    "Doug van Vianen" <courses@shaw.c a> wrote in
    news:NqBvf.2584 56$ki.258172@pd 7tw2no:
    [color=blue]
    > Can someone help me with this problem? Is there a better way to do the
    > same thing?[/color]

    I am *guessing* that under your IE "Internet Options" / Temporary
    Internet Files / Settings, you have your 'check for newer versions of
    stored pages' set to "Every Visit To The Page'.

    This tends to cause an image to flash/disappear each time it is changed
    via javascript - nothing is cached so it must reload, THEN go through
    your transformation. You can either change your settings, or pretty
    much live with it. There *are* some workarounds if you look hard enough
    on google... most involve changing some settings in .htaccess

    You can confirm whether or not this is your problem by looking at the
    page in Firefox or changing your settings in IE.




    Comment

    • web.dev

      #3
      Re: Changing image width using JavaScript


      Doug van Vianen wrote:[color=blue]
      > <script language="JavaS cript">[/color]

      The language attribute is deprecated, use the type attribute as
      follows:

      type = "text/javascript"
      [color=blue]
      > <!--[/color]

      HTML comments are unnecessary.
      [color=blue]
      > document.pic1.w idth = widthf;
      > document.pic1.h eight = heightf;
      > document.pic2.w idth = widthf;
      > document.pic2.h eight = heightf;[/color]

      To affect the width and the height, you also need to access the style
      property:

      document.picX.s tyle.width = widthf;
      document.picX.s tyle.height = heightf;
      [color=blue]
      > <body onLoad="Init(); return true">[/color]

      There is need to return a boolean value. Typically it's also a better
      idea to programmaticall y assign a function reference through javascript
      rather than through the body onload attribute.

      window.onload = Init;
      [color=blue]
      > <div id=div1[/color]

      It's good practice to place quotes around the values.

      <div id = "div1" ...
      [color=blue]
      > <a href="JavaScrip t:void(0)" onClick="ChgSiz e(1); return true">[/color]

      The use of javascript pseudo protocol is highly frowned upon.
      [color=blue]
      ><img name="pic1" border=0 src="pic1.jpg" width=700 height=400></a>[/color]

      Why not try placing the onclick on the img instead?

      <img src = "pic1.jpg" onclick = "ChgSize(1) ">

      Comment

      • Doug van Vianen

        #4
        Re: Changing image width using JavaScript

        Hi,

        Thank you for your reply.

        I checked the settings for my IE and they were okay. In any case, I would
        like the coding to work for anyone's browser without them having to change
        settings. (Great idea but a little unrealistic!)

        I changed the coding to use a series of DIVs, each with the picture to be
        shrunk or expanded at a different level of shrinking or expansion. To do the
        shrinking or expanding I just change which of the DIVs is visible. While
        this is not very sophistocated it seems to work. I have not yet, however,
        checked this on other computers.

        Thank you again.

        Doug vV

        "Good Man" <heyho@letsgo.c om> wrote in message
        news:Xns9743B05 3BD7C2sonicyout h@216.196.97.13 1...[color=blue]
        > "Doug van Vianen" <courses@shaw.c a> wrote in
        > news:NqBvf.2584 56$ki.258172@pd 7tw2no:
        >[color=green]
        >> Can someone help me with this problem? Is there a better way to do the
        >> same thing?[/color]
        >
        > I am *guessing* that under your IE "Internet Options" / Temporary
        > Internet Files / Settings, you have your 'check for newer versions of
        > stored pages' set to "Every Visit To The Page'.
        >
        > This tends to cause an image to flash/disappear each time it is changed
        > via javascript - nothing is cached so it must reload, THEN go through
        > your transformation. You can either change your settings, or pretty
        > much live with it. There *are* some workarounds if you look hard enough
        > on google... most involve changing some settings in .htaccess
        >
        > You can confirm whether or not this is your problem by looking at the
        > page in Firefox or changing your settings in IE.
        >
        >
        >
        >[/color]


        Comment

        • Doug van Vianen

          #5
          Re: Changing image width using JavaScript

          Hi,

          Thank you for your reply. I will update my coding and perhaps also update
          my library of books on JavaScript.

          Doug vV

          "web.dev" <web.dev.cs@gma il.com> wrote in message
          news:1136593407 .998621.63280@g 47g2000cwa.goog legroups.com...[color=blue]
          >
          > Doug van Vianen wrote:[color=green]
          >> <script language="JavaS cript">[/color]
          >
          > The language attribute is deprecated, use the type attribute as
          > follows:
          >
          > type = "text/javascript"
          >[color=green]
          >> <!--[/color]
          >
          > HTML comments are unnecessary.
          >[color=green]
          >> document.pic1.w idth = widthf;
          >> document.pic1.h eight = heightf;
          >> document.pic2.w idth = widthf;
          >> document.pic2.h eight = heightf;[/color]
          >
          > To affect the width and the height, you also need to access the style
          > property:
          >
          > document.picX.s tyle.width = widthf;
          > document.picX.s tyle.height = heightf;
          >[color=green]
          >> <body onLoad="Init(); return true">[/color]
          >
          > There is need to return a boolean value. Typically it's also a better
          > idea to programmaticall y assign a function reference through javascript
          > rather than through the body onload attribute.
          >
          > window.onload = Init;
          >[color=green]
          >> <div id=div1[/color]
          >
          > It's good practice to place quotes around the values.
          >
          > <div id = "div1" ...
          >[color=green]
          >> <a href="JavaScrip t:void(0)" onClick="ChgSiz e(1); return true">[/color]
          >
          > The use of javascript pseudo protocol is highly frowned upon.
          >[color=green]
          >><img name="pic1" border=0 src="pic1.jpg" width=700 height=400></a>[/color]
          >
          > Why not try placing the onclick on the img instead?
          >
          > <img src = "pic1.jpg" onclick = "ChgSize(1) ">
          >[/color]


          Comment

          Working...