Skip to content

Instantly share code, notes, and snippets.

@henrahmagix
Forked from benfoxall/gist:4217317
Created December 5, 2012 22:37
Show Gist options
  • Save henrahmagix/4220142 to your computer and use it in GitHub Desktop.
Save henrahmagix/4220142 to your computer and use it in GitHub Desktop.
DOMNodeInserted
(function($) { $(function() {
// All the nyans.
var nyan = $('.nyan');
var nyanImg = '<img width="40px" src="http://i1.kym-cdn.com/photos/images/original/000/117/424/tumblr_ljwaimhJcK1qa4ebfo1_500.gif" />';
var mouseStillDown = false;
var firstMouseDown = false;
var insertingNyan = false;
var button = $('.button');
// Make the timings editable.
window.nyan = {
firstDelay: 600,
delay: 200
};
var nyanInsert = function() {
insertingNyan = true;
if (mouseStillDown && insertingNyan) {
nyan.append(nyanImg);
setTimeout(nyanInsert, (firstMouseDown) ? window.nyan.firstDelay : window.nyan.delay);
}
insertingNyan = firstMouseDown = false;
};
button.mousedown(function(e) {
mouseStillDown = firstMouseDown = true;
nyanInsert();
});
button.mouseup(function(e) {
mouseStillDown = false;
});
// The counter script.
var insertCount = 0;
var counter = $('.counter');
document.addEventListener('DOMNodeInserted', function(e) {
// Only increment if the node being inserted into isn't the counter.
if (e.relatedNode.id !== 'dom-insert-counter') {
insertCount++;
counter.text(insertCount);
}
}, false);
}); })(jQuery);
body {
background-color: #B9B79E;
color: #2C241D;
font-family: "PT Sans", Arial;
font-size: 12px;
font-weight: normal;
}
code {
background-color: #EDEDE7;
color: #2C241D;
padding: 0 0.5em;
}
a {
color: inherit;
cursor: pointer;
text-decoration: inherit;
}
.title, .subtitle, .button {
font-family: "Arvo";
text-align: center;
}
.counter {
font-size: 24px;
margin-bottom: 10px;
text-align: center;
}
.left {
float: left;
margin-left: 5%;
}
.right {
float: right;
margin-right: 5%;
}
.button {
cursor: pointer;
font-size: 16px;
line-height: 16px;
margin: 0 auto 30px;
}
.side-by-side {
margin: 0 auto;
position: relative;
text-align: center;
}
.info {
color: #2C241D;
}
.clearfix:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
.clearfix {
display: inline-block;
}
.button {
background-color: #EDEDE7;
border: 1px solid #593723;
-moz-box-shadow: 0 10px 0 0 #593723, 0 10px 15px 2px rgba(0, 0, 0, 0.8);
-webkit-box-shadow: 0 10px 0 0 #593723, 0 10px 15px 2px rgba(0, 0, 0, 0.8);
box-shadow: 0 10px 0 0 #593723, 0 10px 15px 2px rgba(0, 0, 0, 0.8);
color: #593723;
display: block;
padding: 10px;
position: relative;
top: 0;
text-decoration: none;
width: 120px;
}
.button:hover, .button:focus {
-moz-box-shadow: 0 2px 0 0 #593723, 0 2px 2px 1px rgba(0, 0, 0, 0.8);
-webkit-box-shadow: 0 2px 0 0 #593723, 0 2px 2px 1px rgba(0, 0, 0, 0.8);
box-shadow: 0 2px 0 0 #593723, 0 2px 2px 1px rgba(0, 0, 0, 0.8);
top: 8px;
}
.button:active {
-moz-box-shadow: inset 0 2px 2px 1px #593723, 0 0 0 0 rgba(0, 0, 0, 0.1);
-webkit-box-shadow: inset 0 2px 2px 1px #593723, 0 0 0 0 rgba(0, 0, 0, 0.1);
box-shadow: inset 0 2px 2px 1px #593723, 0 0 0 0 rgba(0, 0, 0, 0.1);
top: 10px;
}
.button:before {
content: '';
background-color: transparent;
display: block;
position: absolute;
bottom: 100%;
left: 0;
height: 0;
width: 100%;
}
.button:hover:before, .button:focus:before, .button:active:before {
border: 1px solid transparent;
left: -1px;
}
.button:hover:before, .button:focus:before {
height: 8px;
}
.button:active:before {
height: 10px;
}
.animated {
-webkit-transition: all .1s ease;
-moz-transition: all .1s ease;
-ms-transition: all .1s ease;
-o-transition: all .1s ease;
transition: all .1s ease;
}
.animated:active {
-webkit-transition: all 0s ease;
-moz-transition: all 0s ease;
-ms-transition: all 0s ease;
-o-transition: all 0s ease;
transition: all 0s ease;
}
<html>
<head>
<title>DOM Insert Counter</title>
<link href="http://fonts.googleapis.com/css?family=Arvo" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=PT+Sans" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="index.css">
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="dom-insert-counter.js"></script>
</head>
<body>
<h1 class="title">DOM Insert Counter</h1>
<h2 class="subtitle">A script that counts <code>DOMNodeInserted</code> events being fired.</h2>
<p class="subtitle">(apart from the counter updating)</p>
<div class="counter" id="dom-insert-counter">0</div>
<a class="button animated">NYAN</a>
<div class="nyan"></div>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment