2

I am making a user interface where I need to highlight a cell in a table. I have already used coloring for something else, so I need to use the borders. I have created the css to change the borders to dashed, but that doesn't highlight much. I would like to know if there is any way to animate the borders so that they would flash, or circle (typical dashed border animation). I tried with @keyframes and animation, but failed miserably.

I am happy to do it with javascript do it, but I can't use jQuery or any other framework.

If anyone has any other suggestion for highlighting a cell, that could help too.

A test code I use is this:

<table class="t">
    <tr>
        <td class="t">1</td>
        <td class="t top">2</td>
        <td class="t">3</td>
    </tr>
    <tr>
        <td class="t left">4</td>
        <td class="t middle">5</td>
        <td class="t right">6</td>
    </tr>
    <tr>
        <td class="t">7</td>
        <td class="t bottom">8</td>
        <td class="t">9</td>
    </tr>
</table>    

CSS:

table.t {
    border-collapse: collapse;
    border:1px;
    table-layout:fixed;
}

td.t {
    border: 1px solid black;
    width: 50px;
}

td.top {
    border-bottom: 1px dashed black;
}
td.left {
    border-right: 1px dashed black;
}
td.right {
    border-left: 1px dashed black;
}
td.bottom {
    border-top: 1px dashed black;
}
td.middle {
    border: 1px dashed black;
}

Jsfiddle available HERE

4
  • this post as a css3 solution: stackoverflow.com/questions/275931/…
    – T J
    Commented Mar 22, 2014 at 16:40
  • @TilwinJoy, you mean javascript via jquery or do you point at one peticular answer ?
    – G-Cyrillus
    Commented Mar 22, 2014 at 17:11
  • @GCyrillus Vinay's answer for that post to be exact..
    – T J
    Commented Mar 22, 2014 at 17:18
  • @TilwinJoy, thx , it was so crowded of answers :)
    – G-Cyrillus
    Commented Mar 22, 2014 at 17:20

3 Answers 3

5

Not sure exactly what you're looking for, but here's a simple keyframe that applies a box shadow and one that changes transparency... You'll have to add the appropriate vendor prefixing, of course.

CSS

td.selected {
    -webkit-animation: pulse-border 1s infinite;
}

// pulsing border
@-webkit-keyframes pulse-border {
    from, to { box-shadow: 0 0 0 0 black;}
    50% { box-shadow: 0 0 0 4px black; }
}

// flash the cell contents if applied to cell
@-webkit-keyframes flash {
    from, to { opacity: 1 }
    50% { opacity: 0 } 
}

DEMO

edit

Also made this one for fun... A rotating type border. DEMO... Just kind of sucks because of all the extra elements.

6
  • 1
    just to offer an alternative jsfiddle.net/2822m/2 will have to style the surroundings accordingly
    – Kiee
    Commented Mar 22, 2014 at 16:30
  • 1
    Yeah, I'd probably use transparent as the border color rather than white though.
    – brbcoding
    Commented Mar 22, 2014 at 16:34
  • Valid point, that would make better sense, was just a quick example. :)
    – Kiee
    Commented Mar 22, 2014 at 16:35
  • That's awesome. I'll leave the question open a little bit in case anyone has any other idea. But that should work. Thanks
    – d--b
    Commented Mar 22, 2014 at 16:37
  • 1
    @brbcoding lol, for the fun one, my version with gradients : codepen.io/anon/pen/rHxCg . By the way, -webkit- prefix , makes it very restrictive
    – G-Cyrillus
    Commented Mar 22, 2014 at 17:44
1

you can use border image, gradients, en even mutiple box-shadow to draw borders.

animation or transition on hover can animate them too.

examples with gradient : http://codepen.io/gc-nomade/pen/jdwgG and for the fun too , the animated version http://codepen.io/anon/pen/rHxCg

or with box shadow (sorry for the flashy) : http://codepen.io/gcyrillus/pen/bGFLA

you also have outline and outline-offset http://jsfiddle.net/2822m/4/

table.t {
    border-collapse: collapse;
    border:1px;
    table-layout:fixed;
}
td.t {
    border:1px solid;
    width: 50px;
}
td.top {
    border-bottom: none;
}
td.left {
    border-right:none;
}
td.right {
    border-left: none;
}
td.bottom {
    border-top: none;
}
td.middle {
    border: 1px solid red;
    outline:1px dashed black;
    outline-offset:-1px;
}

animation is then easy http://jsfiddle.net/2822m/5/

td.middle {
    border: 1px solid red;
    outline:1px dashed black;
    outline-offset:-1px;
    animation:blink 1s infinite;
}
@keyframes blink {
    50% {
        outline:1px dashed yellow;
    }
}
1

Simple JavaScript answer:

var blinkStatus = 'off',
    currentBlinkElement = null,
    blinkTimer,
    blinkSpeed = 500,
    stopBlinkButton = document.getElementById( 'stopBlinkButton' );

function blink( element )
{
    currentBlinkElement = element;
    if( blinkStatus == 'off' ) {
        currentBlinkElement.className = currentBlinkElement.className  + ' blink-on';
        blinkStatus = 'on';
    } else {
        currentBlinkElement.className = currentBlinkElement.className.replace( ' blink-on', '' );
        blinkStatus = 'off';
    }
    blinkTimer = setTimeout( function(){ blink( element ); }, blinkSpeed );
}

function stopBlink()
{   
    clearTimeout( blinkTimer );
    if( currentBlinkElement != null ) {
        currentBlinkElement.className = currentBlinkElement.className.replace( ' blink-on',  '' );
        currentBlinkElement = null;
    }
}

document.onclick = function( e ) {
    var clickedElement;
    if(e == null) {
        clickedElement = event.srcElement;
    } else {
        clickedElement = e.target;
    }

    if( clickedElement.tagName == 'TD' ) {
        stopBlink();
        blink( clickedElement );
    }
};

stopBlinkButton.onclick = function(){
    stopBlink();
};

JSFiddle working example: http://jsfiddle.net/2Vfu5/. Click a table cell to start.

Not the answer you're looking for? Browse other questions tagged or ask your own question.