• 0

JavaScript DIV position


Question

Hello gang,

 

I have an interesting situation.  I have some Javascript that is working, when used with a HTML file, but for reasons I am not getting, when moved into an asp.net page the position is not working.  The div is being placed at the 0,0 location.  The other properties are working... so what the $%&@ am I doing wrong?

 

Thanks

        var oDiv = document.createElement('div');

        iSize = Math.floor((Math.random() * 32) + 20);
        iRandom = Math.floor((Math.random() * 32) + -32);
        
        iTempTop = ((iTop * i) + iRandom);


        oDiv.className = "BgBubble";
        oDiv.style.position = "absolute";
        oDiv.style.left = 100; //iCenter + iRandom;
        oDiv.style.top = 100; //iTempTop
        oDiv.style.backgroundColor = HexColor;
        oDiv.style.boxShadow =
			'0 0 ' + (iSize + (iSize * 2)) + 'px ' + iSize + 'px ' + HexColor +
			', 0 0 ' + (iSize + (iSize * 3)) + 'px ' + (iSize + (iSize * 2)) + 'px ' + HexColor +
			', 0 0 ' + (iSize + (iSize * 4)) + 'px ' + (iSize + (iSize * 3)) + 'px ' + HexColor;
        oDiv.style.height = iSize;
        oDiv.style.width = iSize;
        oDiv.zIndex = -1;

        document.getElementsByTagName('body')[0].appendChild(oDiv);
Link to comment
https://www.neowin.net/forum/topic/1214843-javascript-div-position/
Share on other sites

9 answers to this question

Recommended Posts

  • 0
  On 23/05/2014 at 16:58, Jose_49 said:

Have you tried it on another browser? 

 

What about using DragonFly, Firebug, or Debug (IE)

 

Thanks, but yea I gave that a try.    To be specific; The code used with a test HTML file, which has the exact same tags, etc as the ASP.Net page, works fine in IE, Chrome or FF.   When I attempt the same output, even if I hard code the Top and Left values, the divs allways end up at 0 and 0.

  • 0
  On 23/05/2014 at 17:28, Jose_49 said:

Have you tried try and catch statements?

 

 

I had not, but I just did and no errors are reported.  I had stepped through the code in Firebug and the lines execute without issue.  Here is the entire function

function loadBubble(iCenter, iColor) {

    try {
        var iSize = 4;
        var frequency = .3;
        var iTop = (window.innerHeight / 10);
        var red = Math.sin(frequency * iColor + 0) * 127 + 128;
        var green = Math.sin(frequency * iColor + 2) * 127 + 128;
        var blue = Math.sin(frequency * iColor + 4) * 127 + 128;
        var HexColor = RGB2Color(red, green, blue);
        var CenterColor = HexColor;
        var iRandom = 0;


        var iTempTop = 0;

        for (var i = 0; i < 8; i++) {
            var oDiv = document.createElement('div');

            iSize = Math.floor((Math.random() * 32) + 20);
            iRandom = Math.floor((Math.random() * 32) + -32);

            iTempTop = ((iTop * i) + iRandom);
            //alert('TempTop: ' + iTempTop);


            //oDiv.className = "BgBubble";
            oDiv.style.position = "absolute";
            oDiv.style.left = 300; //iCenter + iRandom;
            oDiv.style.top = 300; ///iTempTop;  //(iTop * i) + iRandom;
            oDiv.style.backgroundColor = HexColor;
            oDiv.style.boxShadow =
                '0 0 ' + (iSize + (iSize * 2)) + 'px ' + iSize + 'px ' + HexColor +
                ', 0 0 ' + (iSize + (iSize * 3)) + 'px ' + (iSize + (iSize * 2)) + 'px ' + HexColor +
                ', 0 0 ' + (iSize + (iSize * 4)) + 'px ' + (iSize + (iSize * 3)) + 'px ' + HexColor;
            oDiv.style.height = iSize;
            oDiv.style.width = iSize;
            oDiv.zIndex = -1;

            document.getElementsByTagName('body')[0].appendChild(oDiv);
        }
    }
    catch (err) {
        alert('Error: ' + err.message);
    }
}
  • 0

Hmmm. Just out of curiosity... Have you tried adding "px" at the end of each line?

 

 oDiv.style.left = "300 px"; //iCenter + iRandom;

oDiv.style.top = "300 px"; ///iTempTop; //(iTop * i) + iRandom;

 

try this:

 

oDiv.style.left = iCenter + iRandom + 'px';

oDiv.style.top = iTempTop + 'px';

  • 0
  On 23/05/2014 at 20:46, Jose_49 said:

Hmmm. Just out of curiosity... Have you tried adding "px" at the end of each line?

 

 oDiv.style.left = "300 px"; //iCenter + iRandom;

oDiv.style.top = "300 px"; ///iTempTop; //(iTop * i) + iRandom;

 

try this:

 

oDiv.style.left = iCenter + iRandom + 'px';

oDiv.style.top = iTempTop + 'px';

 

 

ohhh goodness.  I'm an idiot.  I have just spent the past 90 minutes making a testbed with the basics, and it worked in HTML and not the ASP.  And litterally as I was entering a new post with the source code you post this.  I tested it and TaDa!

 

Yep folks, I'm an idiot.  Thank you very much Jose.   Have a great weekend

  • 0
  On 23/05/2014 at 20:49, James Rose said:

ohhh goodness.  I'm an idiot.  I have just spent the past 90 minutes making a testbed with the basics, and it worked in HTML and not the ASP.  And litterally as I was entering a new post with the source code you post this.  I tested it and TaDa!

 

Yep folks, I'm an idiot.  Thank you very much Jose.   Have a great weekend

Hahah

 

Nah man. I'm glad I've helped you. 

 

This has happened me in the past and will keep happening, because one kinds of smoke some rare weed or something....

This topic is now closed to further replies.
  • Posts

    • The notch on laptops is still the silliest thing I have ever seen. They should work on eliminating it. You're going to need to connect a monitor just to get a full view of your screen without a stupid hole in it.
    • Bad news for HBO Max users: the company's about to shut down password sharing "aggressively" by David Uzondu If you have an HBO Max account and share the password with a couple of friends, that free ride is about to end. The company will get more "aggressive," according to JB Perrette, the head of streaming and games at Warner Bros. Discovery. Up till now, the company has sent a gentler, cancelable message to users it identified as being in the "higher tier of usage". This soft approach was a simple warning shot, letting the biggest offenders know that a change was coming without immediately cutting off their access to shows like House of the Dragon or The Last of Us. The real crackdown, however, is scheduled to begin next month, with enforcement tightening throughout the rest of the year. If you are wondering why this is happening now, well, it worked for Netflix, as the streaming giant saw a growth in U.S. gross additions of over 128.9% in the month after its crackdown, with nearly 3.5 million signups in June 2023 alone. By the end of 2023, the company added a whopping 13.1 million subscribers in just the last three months. Warner Bros. Discovery has clearly taken notes and wants a piece of that pie. JB Perrette claimed that the company can now determine who is legitimate and who's not, distinguishing between actual travel and account sharing as a result of months of testing its detection filters. A more forceful approach will begin in the fourth quarter of the year (October-December), when messages will require action. The company is confident its system can accurately pinpoint password moochers without penalizing paying customers who are just on vacation. Warner Bros. Discovery's plan to give freeloaders a legitimate path forward is an option to add a member for a fee, which it launched as "Extra Member Add-On" for $7.99 a month in April this year. This add-on gives a non-household user their own profile and login under the main account, a direct way to convert borrowers into paying customers. Via: Deadline
    • Sweet, giving the updated versions to og owners for free to. Finally a bit more pro-consumer news for a change.
  • Recent Achievements

    • First Post
      EzraNougat earned a badge
      First Post
    • One Month Later
      westDvina earned a badge
      One Month Later
    • Community Regular
      Bern@rd went up a rank
      Community Regular
    • Week One Done
      Joey Solo earned a badge
      Week One Done
    • Week One Done
      Vicente C Alves earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      714
    2. 2
      +FloatingFatMan
      203
    3. 3
      ATLien_0
      163
    4. 4
      Xenon
      130
    5. 5
      wakjak
      110
  • Tell a friend

    Love Neowin? Tell a friend!