1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

To to write this part of the script to be better novice readable

Discussion in 'JavaScript' started by ketting00, Apr 13, 2014.

  1. #1
    I picked this up from the internet and after a whole day long trying to dissect the script here's the part I don't understand.
    
    $('.wrapper').html('<div id="slice"><div class="pie"></div>'+(percent > 50?'<div class="pie fill"></div>':'')+'</div>');
    
    Code (markup):
    How to rewrite it something like this:
    
    if (percent > 50) {
       $('#slice').css({
         'clip':'rect(auto,auto,auto,auto)'
       });
       $('#slice').addClass('fill');
    }
    
    Code (markup):
    The part which I don't understand is <div class="pie fill"></div>. How do you write it in jQuery. (I've tried $('#slice').addClass('pre fill'); a hundred time already.)
    The CSS part of the code:
    
    .pie {
       border: 0.1em solid #800;
       position: absolute;
       width: 0.8em;
       height: 0.8em;
       clip: rect(0em,0.5em,1em,0em);
       -webkit-border-radius: 0.5em;
        -moz-border-radius: 0.5em;
           border-radius: 0.5em;
       z-index: 110;
    }
    .fill {
       -webkit-transform: rotate(180deg) !important;
        -moz-transform: rotate(180deg) !important;
          -o-transform: rotate(180deg) !important;
           transform: rotate(180deg) !important;
    }
    
    Code (markup):
    They're working as expected when using the code which I don't understand.

    Thank you
     
    ketting00, Apr 13, 2014 IP
  2. deathshadow

    deathshadow Prominent Member

    Messages:
    6,610
    Likes Received:
    984
    Best Answers:
    164
    Trophy Points:
    395
    #2
    Welcome to what I mean by jQuery being needlessly and pointlessly cryptic AND promoting bad/broken methodologies.

    See the ? and the : inside the working example? That's a conditional.

    alert(true ? '1' : '0'); // will output '1'
    alert(false ? '1' : '0'); // will output '0'

    So the trick is to look at what's being generated for markup (which shouldn't be done) by that jquery crap.

    If it's <= 50 the markup made by that first code would be:
    <div id="slice"><div class="pie"></div></div>

    If it's > 50 the markup made by it is:
    <div id="slice"><div class="pie"></div><div class="pie fill"></div></div>

    ... and that's why your code isn't working, you aren't adding a div called "pie fill", you're adding a class to an existing element; the parent of both. NOT that I grasp what the devil it needs two DIV.pie for in the first place... Or why you'd be doing this client-side with only 50% accuracy... or why it seems to be screwing around with 'clip' ...

    Could you put a copy up live somewhere or even just screencaps of what you want it to do? I've got the feeling that whatever it is you're attempting to do here, it could be done far, far, far simpler.

    ... and if that's being updated client-side due to percent changing, getting rid of the content generation (which basically uses innerHTML) would probably help the performance of it.

    Really I think I'd need to see more to advise further.
     
    deathshadow, Apr 13, 2014 IP
  3. ketting00

    ketting00 Active Member

    Messages:
    414
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    68
    #3
    Thank for response.
    What's a clever coding. I haven't seen something like this before.

    Here's what I'm trying to do. I've cut off all unnecessary elements:
    
    <div class="wrapper">
       <div class="circle">
         <a href="#"><img src="<?php echo the_image(); ?>" alt="" /></a>
       </div>
       <div id="slice" class="gt50">
         <div class="pie"></div>
       </div>
    </div>
    
    Code (markup):
    The JQuery:
    
    var timer;
    var timerFinish;
    var timerSeconds;
    function drawTimer(percent){
       // uncomment this and comment or remove the if condition below and the border line goes in circle
       // $('.circle-wrapper').html('<div id="slice"'+(percent > 50?' class="gt50"':'')+'><div class="pie"></div>'+(percent > 50?'<div class="pie fill"></div>':'')+'</div>');
    
    
       var deg = 360/100*percent;
       $('#slice .pie').css({
         '-webkit-transform':'rotate('+deg+'deg)',
         '-moz-transform':'rotate('+deg+'deg)',
         '-o-transform':'rotate('+deg+'deg)',
         'transform':'rotate('+deg+'deg)'
       });
       // This is what I'm trying to make it worked without success
       if (percent > 50) {
         // I removed the #slice.gt50 selector and replace it with this
         $('#slice .pie').css({
           'clip':'rect(0em,0.5em,1em,0em)'
         });
         $('#slice').addClass('fill');
       }
    }
    
    function stopTimer(){
       var seconds = (timerFinish-(new Date().getTime()))/1000;
       if(seconds <= 0){
         clearInterval(timer);
         launching_nuclear_strike();
       }else{
         var percent = 100-((seconds/timerSeconds)*100);
         drawTimer(percent);
       }
    }
    $(document).ready(function(){
       (function startTimer() {
         timerSeconds = 10;
         timerFinish = new Date().getTime()+(timerSeconds*1000);
         timer = setInterval('stopTimer()',50);
       })();
    });
    
    Code (markup):
    The CSS:
    
    .wrapper {
       position: absolute;
       font-size: 65px;
       width: 1em;
       height: 1em;
    }
    .circle {
       width: 50px;
       height: 50px;
       position: absolute;
    }
    .circle img {
       width: 50px;
       height: 50px;
       border-radius: 50px;
    }
    #slice {
       position: absolute;
       width: 1em;
       height: 1em;
       clip: rect(0px,1em,1em,0.5em);
    }
    /* In practice I'd remove the below selector */
    #slice.gt50 {
       clip: rect(auto, auto, auto, auto);
    }
    .pie {
       border: 0.1em solid #c0c0c0;
       position: absolute;
       width: 0.8em;
       height: 0.8em;
       clip: rect(0em,0.5em,1em,0em);
       -webkit-border-radius: 0.5em;
        -moz-border-radius: 0.5em;
           border-radius: 0.5em;
    }
    .fill {
       -webkit-transform: rotate(180deg) !important;
        -moz-transform: rotate(180deg) !important;
          -o-transform: rotate(180deg) !important;
           transform: rotate(180deg) !important;
    }
    
    Code (markup):
    It's a circular progress bar. On his web site the author appends a percentage count to the window. In my project I want it to circle round a static object, say an image. If you make the progress bar transparent you'd see 2 moving parts working in sequence.

    My condition is do not append an object to the window (use z-index instead). So this is a challenge. If success I could begin to launch a nuclear strike simultaneously with the ending of the progress bar ;)

    The link: http://blakek.us/labs/jquery/css3-pie-graph-timer/
     
    Last edited: Apr 13, 2014
    ketting00, Apr 13, 2014 IP
  4. deathshadow

    deathshadow Prominent Member

    Messages:
    6,610
    Likes Received:
    984
    Best Answers:
    164
    Trophy Points:
    395
    #4
    IF I'm grasping what it is you are trying to do (Not sure I am) --- a simple 0.1em wide line that rotates inside the container... you got WAY too compilcated for your own good on this; not exactly surprising with the steaming train wreck of nonsense known as jQuery involved. You have static markup of scripting only elements, far, FAR too much playing with scripting on this, and far too much CSS for something so simple as well.

    I'm really not sure why the 50% mark is your breakpoint... could you explain that better or put a live copy up of what you have working so I can see what you are trying to do here?

    With CSS3 on the table, you might even be able to ditch almost ALL the CSS and most of those DIV for nothing in the markup if you don't care about IE9/earlier. Since you're already using transform, that's really not a big change.

    Though yeah, the original code of constantly flushing child nodes and then building child nodes with what for all intents and purposes is innerHTML is REALLY bad. Entirely what I expect from jQuerytards though.

    Part of why I don't advocate the use of the fat bloated train wreck of idiocy known as jQuery in the first place.
     
    deathshadow, Apr 13, 2014 IP
  5. ketting00

    ketting00 Active Member

    Messages:
    414
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    68
    #5
    Hi,

    I've updated the data. So you could see the link where I get the code from.
    No, I don't care about IE9 and its predecessors. I would css it in another way. I don't want to use jquery, but this is a wordpress theme. So be it. Wordpress obviously fond of jquery.

    This is a second day attempt. If fail again I would ditch it. But this thing is tempting. It's nice to have it on a wordpress site.

    Thank you,
     
    ketting00, Apr 13, 2014 IP
  6. ketting00

    ketting00 Active Member

    Messages:
    414
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    68
    #6
    Ha! Got it worked. Now that it's simple.
     
    ketting00, Apr 13, 2014 IP