<html>
<head>
<script src="https://code.jquery.com/jquery-1.8.1.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<div class="news-holder">
<ul class="news-headlines">
<li class="selected">100 red bicycles stolen from local bike store</li>
<li>New leash laws in effect for floppy-eared dogs</li>
<li>Insider: Can palm trees be saved?</li>
<li>Fresh recipes to titillate the taste buds</li>
<li>Truck inspections under way for the metropolitan area</li>
<li>Are the beaches safe for swimming this year?</li>
<li class="highlight"></li>
</ul>
<div class="news-preview">
<div class="news-content top-content">
<img src="http://cdn.impressivewebs.com/news1.jpg">
<p><a href="#">100 red bicycles stolen from local bike store</a></p>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
</div><!-- .content-1 -->
<div class="news-content">
<img src="http://cdn.impressivewebs.com/news2.jpg">
<p><a href="#">New leash laws in effect for floppy-eared dogs</a></p>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
</div><!-- .content-2 -->
<div class="news-content">
<img src="http://cdn.impressivewebs.com/news3.jpg">
<p><a href="#">Insider: Can palm trees be saved?</a></p>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
</div><!-- .content-3 -->
<div class="news-content">
<img src="http://cdn.impressivewebs.com/news4.jpg">
<p><a href="#">Fresh recipes to titillate the taste buds</a></p>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
</div><!-- .content-4 -->
<div class="news-content">
<img src="http://cdn.impressivewebs.com/news5.jpg">
<p><a href="#">Truck inspections under way for the metropolitan area</a></p>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
</div><!-- .content-5 -->
<div class="news-content">
<img src="http://cdn.impressivewebs.com/news6.jpg">
<p><a href="#">Are the beaches safe for swimming this year?</a></p>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
</div><!-- .content-6 -->
</div><!-- .news-preview -->
</div><!-- .news-holder -->
</body>
</html>
/*globals $, clearInterval, setInterval, window, setTimeout */
$(function () {
"use strict";
var hl = $('.highlight'),
newsList = $('.news-headlines'),
newsListItems = $('.news-headlines li'),
firstNewsItem = $('.news-headlines li:nth-child(1)'),
newsPreview = $('.news-preview'),
elCount = $('.news-headlines').children(':not(.highlight)').index(),
vPadding = (parseInt(firstNewsItem.css('padding-top').replace('px', ''), 10)) +
(parseInt(firstNewsItem.css('padding-bottom').replace('px', ''), 10)),
vMargin = (parseInt(firstNewsItem.css('margin-top').replace('px', ''), 10)) +
(parseInt(firstNewsItem.css('margin-bottom').replace('px', ''), 10)),
myTimer = null,
siblings = null,
totalHeight = null,
indexEl = 1,
i = null;
function doEqualHeight() {
if (newsPreview.height() < newsList.height()) {
newsPreview.height(newsList.height());
} else if ((newsList.height() < newsPreview.height()) && (newsList.height() > parseInt(newsPreview.css('min-height').replace('px', ''), 10))) {
newsPreview.height(newsList.height());
}
}
function doTimedSwitch() {
myTimer = setInterval(function () {
if (($('.selected').prev().index() + 1) === elCount) {
firstNewsItem.trigger('click');
} else {
$('.selected').next(':not(.highlight)').trigger('click');
}
}, 3000);
}
function doClickItem() {
newsListItems.on('click', function () {
newsListItems.removeClass('selected');
$(this).addClass('selected');
siblings = $(this).prevAll();
totalHeight = 0;
// this loop calculates the height of individual elements, including margins/padding
for (i = 0; i < siblings.length; i += 1) {
totalHeight += $(siblings[i]).height();
totalHeight += vPadding;
totalHeight += vMargin;
}
// this moves the highlight vertically the distance calculated in the previous loop
// and also corrects the height of the highlight to match the current selection
hl.css({
top: totalHeight,
height: $(this).height() + vPadding
});
indexEl = $(this).index() + 1;
$('.news-content:nth-child(' + indexEl + ')').siblings().removeClass('top-content');
$('.news-content:nth-child(' + indexEl + ')').addClass('top-content');
clearInterval(myTimer);
doTimedSwitch();
});
}
function doWindowResize() {
$(window).resize(function () {
clearInterval(myTimer);
setTimeout(function () {
$('.selected').trigger('click');
}, 1000 );
doEqualHeight();
});
}
doClickItem();
doWindowResize();
setTimeout(function () {
doEqualHeight();
}, 500);
$('.selected').trigger('click');
});
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |