Marius van Witzenburg We fight for our survival, we fight!

14jul/110

How to grab the mouse position in a browser with jQuery and HTML5

Posted by mariusvw

Depending on what your plans are it might become handy to know the mouse position.
The script below gives you the position relative to the page, relative to the browser window and relative to the DIV with the green color.

HTML (index.html)

<!DOCTYPE html>
<html>
<head>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="javascript.js"></script>
    <link href="style.css" type="text/css">
</head>
<body>
    <div id="object"></div>
    <div id="page" class="info">Page: <span></span></div>
    <div id="client" class="info">Client: <span></span></div>
    <div id="container" class="info">Container: <span></span></div>
</body>
</html>

CSS (style.css)

* {
    margin: 0px;
    border: 0px;
    padding: 0px;
}
#object {
    width: 595px;
    height: 842px;
    margin: 10px;
    background: #cfc;
    border: 1px solid #666;
    float: left;
}
.info {
    padding-top: 100px;
}

JavaScript (javascript.js)

$(function() {
    $("#object").mousemove(function(e) {
        var pageCoords = "( " + e.pageX + ", " + e.pageY + " )";
        var clientCoords = "( " + e.clientX + ", " + e.clientY + " )";
        var cLeft = e.pageX - $(this).offset().left;
        var cTop = e.pageX - $(this).offset().top;
        var containerCoords = "( " + cLeft + ", " + cTop + " )";
        $("#page span").text("( e.pageX, e.pageY ) - " + pageCoords);
        $("#client span").text("( e.clientX, e.clientY ) - " + clientCoords);
        $("#container span").text("( e.clientX, e.clientY ) - " + containerCoords);
    }).click(function(e) {
        var x = e.pageX - this.offsetLeft;
        var y = e.pageY - this.offsetTop;
        var containerCoords = "( " + x + ", " + y + " )";
        alert("You clicked: ( e.clientX, e.clientY ) - " + containerCoords);
    });
});

Related links:
http://docs.jquery.com/Tutorials:Mouse_Position

Geëtiketeerd als: , , , , Geen reacties
6jul/110

How to change the style of the prefix numbers of a ordered list, OL tag, with CSS

Posted by mariusvw

A solution that most people use is adding a SPAN tag to the list item and style the OL tag first and re-style the SPAN tag.

CSS

ol { font-weight: bold }
ol span { font-weight: normal }

HTML

<ol>
<li><span>Item 1</span></li>
<li><span>Item 2</span></li>
<li><span>Item 3</span></li>
<li><span>Item 4</span></li>
<li><span>Item 5</span></li>
</ol>

But there is another way with only CSS2 stylesheet.

ol { counter-reset: item }
ol li { display: block }
ol li:before {
    content: counter(item) ". ";
    counter-increment: item;
    font-weight: bold;
}

Result of a ordered list without span tags and the above CSS:

  1. Item 1
  2. Item 2
  3. Item 3
  4. Item 4
  5. Item 5

For some more info check w3schools.com page: CSS counter-increment property

Geëtiketeerd als: , , , , Geen reacties
25aug/100

How to fix inconsistent line ending (EOL) style with find and Perl

Posted by mariusvw

If you work with subversion you might get this error when you got files that have been edited on different operating systems like Windows, Linux, FreeBSD or Mac OS X.

Well, the fix is quite simple. You simply replace the wrong line endings with right ones depending of which you want. In my situation I want unix style line endings.

Replace in PHP/JavaScript files:

find ./ -name '*.php' -type f -exec perl -i -wpe 's/rn/n/g' '{}' ;
find ./ -name '*.php' -type f -exec perl -i -wpe 's/r/n/g' '{}' ;
find ./ -name '*.js' -type f -exec perl -i -wpe 's/rn/n/g' '{}' ;
find ./ -name '*.js' -type f -exec perl -i -wpe 's/r/n/g' '{}' ;

In case you want to replace them in multiple file types you can adjust the command. In this example we want to replace in the following file types:

  1. asp
  2. cfm
  3. css
  4. html
  5. js
  6. php
  7. pl
  8. txt

Use the following commands:

find ./ -name '*.asp' -or -name '*.cfm' -or -name '*.css' -or -name '*.html' -or -name '*.js' -or -name '*.php' -or -name '*.pl' -or -name '*.txt' -type f -exec perl -i -wpe 's/rn/n/g' '{}' ;
find ./ -name '*.asp' -or -name '*.cfm' -or -name '*.css' -or -name '*.html' -or -name '*.js' -or -name '*.php' -or -name '*.pl' -or -name '*.txt' -type f -exec perl -i -wpe 's/r/n/g' '{}' ;

Keep in mind, you may use these commands on your own risk. I'm not responsible if you lose your work ;-)

Now you should be able to commit your files again :-)