How to recover an active div - javascript
I try to use the jquery flip to display information on an div flip.
JSFIDDLE
I have a first div called "workListId", under a second div called "workId".
Under the div workId, two another div called "workDataId" and "workFlipId".
Under the div workDataId, two another div called "workDescriptionId" and "workURLId"
I have a problem with the div workDescriptionID.
If I flip the div in the order, I see the description in order too.
But if Iflip the third div(for example), I see the description of the first div.
I think the problem is about this line but I don't know to resolve it :
elt.html($('.workData').html());
So see my code below :
$(function(){
// A - CONSTRUCT THE DIV
for ( var i in works ){
// work div
$('<div/>', {
'id' : 'workId' + i,
'class' : 'work'
}).appendTo("#workListId");
// workFlip div
$('<div/>', {
'id' : 'workFlipId' + i,
'class' : 'workFlip'
}).appendTo("#workId" + i);
// workData div
$('<div/>', {
'id' : 'workDataId' + i,
'class' : 'workData'
}).appendTo("#workId" + i);
// workDescription div
$('<div/>', {
'id' : 'workDescriptionId' + i,
'class' : 'workDescription'
}).appendTo("#workDataId" + i);
// workURL div
$('<div/>', {
'id' : 'workURLId' + i,
'class' : 'workURL'
}).appendTo("#workDataId" + i);
// Insert the content in each div
$("#workFlipId" + i).html(path.pathBegin + works[i].image + path.pathEnd);
$("#workDescriptionId" + i).html('<p>' + works[i].description + '</p>');
$("#workURLId" + i).html('Read more...');
}//End for
// B - CONSTRUCT THE DIV FLIP
/* The code is executed once the DOM is loaded */
$('.work').bind("click",function(){
// $(this) point to the clicked .workFlip element (caching it in elem for speed):
var elt = $(this);
// data('flipped') is a flag we set when we flip the element:
if(elt.data('flipped'))
{
// If the element has already been flipped, use the revertFlip method
// defined by the plug-in to revert to the default state automatically:
elt.revertFlip();
// Setting the flag:
elt.data('flipped',true);
}// End if
else
{
// Using the flip method defined by the plugin:
elt.flip({
direction: params.direction,
color: params.color,
speed: params.speed,
onBefore: function(){
// Insert the contents of the .workData div (hidden from view with display:none)
// into the clicked .workFlip div before the flipping animation starts:
elt.html($('.workFlip').siblings('.workData').html());
}
});// End $this.flip
// Setting the flag:
elt.data('flipped',true);
}// End else
});// End function work
});// End function
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="Scripts/jquery-ui-1.8.16.min.js" type="text/javascript"></script>
<script type="text/javascript">
eval(function (p, a, c, k, e, r) { e = function (c) { return (c < a ? '' : e(parseInt(c / a))) + ((c = c % a) > 35 ? String.fromCharCode(c + 29) : c.toString(36)) }; if (!''.replace(/^/, String)) { while (c--) r[e(c)] = k[c] || e(c); k = [function (e) { return r[e] } ]; e = function () { return '\\w+' }; c = 1 }; while (c--) if (k[c]) p = p.replace(new RegExp('\\b' + e(c) + '\\b', 'g'), k[c]); return p } ('(5($){5 H(a){a.1D.1f[a.1E]=1F(a.1G,10)+a.1H}6 j=5(a){1I({1J:"1g.Z.1K 1L 1M",1N:a})};6 k=5(){7(/*#1O!#*/11&&(1P 1Q.1h.1f.1R==="1S"))};6 l={1T:[0,4,4],1U:[1i,4,4],1V:[1j,1j,1W],1X:[0,0,0],1Y:[0,0,4],1Z:[1k,1l,1l],20:[0,4,4],21:[0,0,A],22:[0,A,A],23:[12,12,12],24:[0,13,0],26:[27,28,1m],29:[A,0,A],2a:[2b,1m,2c],2d:[4,1n,0],2e:[2f,2g,2h],2i:[A,0,0],2j:[2k,2l,2m],2n:[2o,0,R],2p:[4,0,4],2q:[4,2r,0],2s:[0,t,0],2t:[2u,0,2v],2w:[1i,1o,1n],2x:[2y,2z,1o],2A:[1p,4,4],2B:[1q,2C,1q],2D:[R,R,R],2E:[4,2F,2G],2H:[4,4,1p],2I:[0,4,0],2J:[4,0,4],2K:[t,0,0],2L:[0,0,t],2M:[t,t,0],2N:[4,1k,0],2O:[4,S,2P],2Q:[t,0,t],2R:[t,0,t],2S:[4,0,0],2T:[S,S,S],2U:[4,4,4],2V:[4,4,0],9:[4,4,4]};6 m=5(a){T(a&&a.1r("#")==-1&&a.1r("(")==-1){7"2W("+l[a].2X()+")"}2Y{7 a}};$.2Z($.30.31,{u:H,v:H,w:H,x:H});$.1s.32=5(){7 U.1t(5(){6 a=$(U);a.Z(a.B(\'1u\'))})};$.1s.Z=5(i){7 U.1t(5(){6 c=$(U),3,$8,C,14,15,16=k();T(c.B(\'V\')){7 11}6 e={I:(5(a){33(a){W"X":7"Y";W"Y":7"X";W"17":7"18";W"18":7"17";34:7"Y"}})(i.I),y:m(i.D)||"#E",D:m(i.y)||c.z("19-D"),1v:c.J(),F:i.F||1w,K:i.K||5(){},L:i.L||5(){},M:i.M||5(){}};c.B(\'1u\',e).B(\'V\',1).B(\'35\',e);3={s:c.s(),n:c.n(),y:m(i.y)||c.z("19-D"),1x:c.z("36-37")||"38",I:i.I||"X",G:m(i.D)||"#E",F:i.F||1w,o:c.1y().o,p:c.1y().p,1z:i.1v||39,9:"9",1a:i.1a||11,K:i.K||5(){},L:i.L||5(){},M:i.M||5(){}};16&&(3.9="#3a");$8=c.z("1b","3b").8(3c).B(\'V\',1).3d("1h").J("").z({1b:"1A",3e:"3f",p:3.p,o:3.o,3g:0,3h:3i});6 f=5(){7{1B:3.9,1x:0,3j:0,u:0,w:0,x:0,v:0,N:3.9,O:3.9,P:3.9,Q:3.9,19:"3k",3l:\'3m\',n:0,s:0}};6 g=5(){6 a=(3.n/13)*25;6 b=f();b.s=3.s;7{"q":b,"1c":{u:0,w:a,x:a,v:0,N:\'#E\',O:\'#E\',o:(3.o+(3.n/2)),p:(3.p-a)},"r":{v:0,u:0,w:0,x:0,N:3.9,O:3.9,o:3.o,p:3.p}}};6 h=5(){6 a=(3.n/13)*25;6 b=f();b.n=3.n;7{"q":b,"1c":{u:a,w:0,x:0,v:a,P:\'#E\',Q:\'#E\',o:3.o-a,p:3.p+(3.s/2)},"r":{u:0,w:0,x:0,v:0,P:3.9,Q:3.9,o:3.o,p:3.p}}};14={"X":5(){6 d=g();d.q.u=3.n;d.q.N=3.y;d.r.v=3.n;d.r.O=3.G;7 d},"Y":5(){6 d=g();d.q.v=3.n;d.q.O=3.y;d.r.u=3.n;d.r.N=3.G;7 d},"17":5(){6 d=h();d.q.w=3.s;d.q.P=3.y;d.r.x=3.s;d.r.Q=3.G;7 d},"18":5(){6 d=h();d.q.x=3.s;d.q.Q=3.y;d.r.w=3.s;d.r.P=3.G;7 d}};C=14[3.I]();16&&(C.q.3n="3o(D="+3.9+")");15=5(){6 a=3.1z;7 a&&a.1g?a.J():a};$8.1d(5(){3.K($8,c);$8.J(\'\').z(C.q);$8.1e()});$8.1C(C.1c,3.F);$8.1d(5(){3.M($8,c);$8.1e()});$8.1C(C.r,3.F);$8.1d(5(){T(!3.1a){c.z({1B:3.G})}c.z({1b:"1A"});6 a=15();T(a){c.J(a)}$8.3p();3.L($8,c);c.3q(\'V\');$8.1e()})})}})(3r);', 62, 214, '|||flipObj|255|function|var|return|clone|transparent||||||||||||||height|top|left|start|second|width|128|borderTopWidth|borderBottomWidth|borderLeftWidth|borderRightWidth|bgColor|css|139|data|dirOption|color|999|speed|toColor|int_prop|direction|html|onBefore|onEnd|onAnimation|borderTopColor|borderBottomColor|borderLeftColor|borderRightColor|211|192|if|this|flipLock|case|tb|bt|flip||false|169|100|dirOptions|newContent|ie6|lr|rl|background|dontChangeColor|visibility|first|queue|dequeue|style|jquery|body|240|245|165|42|107|140|230|224|144|indexOf|fn|each|flipRevertedSettings|content|500|fontSize|offset|target|visible|backgroundColor|animate|elem|prop|parseInt|now|unit|throw|name|js|plugin|error|message|cc_on|typeof|document|maxHeight|undefined|aqua|azure|beige|220|black|blue|brown|cyan|darkblue|darkcyan|darkgrey|darkgreen||darkkhaki|189|183|darkmagenta|darkolivegreen|85|47|darkorange|darkorchid|153|50|204|darkred|darksalmon|233|150|122|darkviolet|148|fuchsia|gold|215|green|indigo|75|130|khaki|lightblue|173|216|lightcyan|lightgreen|238|lightgrey|lightpink|182|193|lightyellow|lime|magenta|maroon|navy|olive|orange|pink|203|purple|violet|red|silver|white|yellow|rgb|toString|else|extend|fx|step|revertFlip|switch|default|flipSettings|font|size|12px|null|123456|hidden|true|appendTo|position|absolute|margin|zIndex|9999|lineHeight|none|borderStyle|solid|filter|chroma|remove|removeData|jQuery'.split('|'), 0, {}))
$(document).ready(function () {
//******************* SETTING THE FLIP ********************//
// direction : Set the direction of the flipped div
// lr : Left to right
// rl : Right to left
// tb : Top to bottom
// bt : Bottom to top
//
// color : Set the color of your div
// speed : Set the time speed to flip (default : 300)
//*********************************************************//
var params = {
direction: 'lr',
color: 'black',
speed: 220
};
//******************* SETTING THE WORKS ********************//
// title : Set the title of your work
// image : Set the image of your work
// description : Set the description of your work
// website : Set the link of your work or your full size image
//
// How to create many works ?
// Simply copy and paste the lines : " title1 : { ... }, "
// after your last works and complete the setting.
//*********************************************************//
var works = {
title1: {
image: 'facebook.png',
description: 'Description One.',
website: 'http://www.facebook.com'
}, // End title1
title2: {
image: 'google.png',
description: 'Description Two.',
website: 'http://www.google.com'
}, // End title2
title3: {
image: 'adobe.png',
description: 'Description Three.',
website: 'http://www.adobe.com'
}// End title3
}; // End var works
//******************* SETTING THE PATH OF WORKS ************//
// pathBegin : Set the path of your works
// pathEnd : DOES NOT CHANGE
//*********************************************************//
var path = {
pathBegin: '<img src="img/works/',
pathEnd: '"/>'
};
// A - CONSTRUCT THE DIV
$.each(works, function (i, work) {
// work div
$('<div/>', {
'id': 'workId' + i,
'class': 'work'
}).appendTo("#workListId");
// workFlip div
$('<div/>', {
'id': 'workFlipId' + i,
'class': 'workFlip'
}).appendTo("#workId" + i);
// workData div
$('<div/>', {
'id': 'workDataId' + i,
'class': 'workData'
}).appendTo("#workId" + i);
// workDescription div
$('<div/>', {
'id': 'workDescriptionId' + i,
'class': 'workDescription'
}).appendTo("#workDataId" + i);
// workURL div
$('<div/>', {
'id': 'workURLId' + i,
'class': 'workURL'
}).appendTo("#workDataId" + i);
// Insert the content in each div
$("#workFlipId" + i).html(path.pathBegin + work.image + path.pathEnd);
$("#workDescriptionId" + i).html('<p>' + work.description + '</p>');
$("#workURLId" + i).html('Read more...');
}); //End foreach
// B - CONSTRUCT THE DIV FLIP
/* The code is executed once the DOM is loaded */
$('.work').bind("click", function () {
// $(this) point to the clicked .workFlip element (caching it in elem for speed):
var elt = $(this);
// data('flipped') is a flag we set when we flip the element:
if (elt.data('flipped')) {
// If the element has already been flipped, use the revertFlip method
// defined by the plug-in to revert to the default state automatically:
elt.revertFlip();
// Unsetting the flag:
elt.data('flipped', true);
} // End if
else {
// Using the flip method defined by the plugin:
elt.flip({
direction: params.direction,
color: params.color,
speed: params.speed,
onBefore: function () {
// Insert the contents of the .workData div (hidden from view with display:none)
// into the clicked .workFlip div before the flipping animation starts:
//elt.html($('.workFlip').siblings('.workData').html()); //This Code Will Take only first div workDescription only.
elt.html(elt.find('.workFlip').siblings('.workData').html());
}
}); // End elt.flip
// Setting the flag:
elt.data('flipped', true);
} // End else
}); // End function work
});
</script>
<style type="text/css">
*
{
/* Resetting the default styles of the page */
margin: 0;
padding: 0;
}
body
{
/* Setting default text color, background and a font stack */
font-size: 0.825em;
color: white;
background-color: #fff;
font-family: Arial, Helvetica, sans-serif;
}
.workList
{
position: relative;
margin: 30px auto;
width: 950px;
}
.work
{
width: 180px;
height: 180px;
float: left;
margin: 5px; /* Giving the work div a relative positioning: */
position: relative;
cursor: pointer;
}
.workFlip
{
/* The work div will be positioned absolutely with respect
to its parent .work div and fill it in entirely */
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
border: 1px solid #ddd;
background: url("../img/background.jpg") no-repeat center center #f9f9f9;
}
.workFlip:hover
{
border: 1px solid #999; /* CSS3 inset shadow: */
-moz-box-shadow: 0 0 30px #999 inset;
-webkit-box-shadow: 0 0 30px #999 inset;
box-shadow: 0 0 30px #999 inset;
}
.workFlip img
{
/* Centering the logo image in the middle of the workFlip div */
position: absolute;
top: 50%;
left: 50%;
margin: -70px 0 0 -70px;
}
.workData
{
/* Hiding the .workData div */
display: none;
}
.workDescription
{
font-size: 11px;
padding: 30px 10px 20px 20px;
font-style: italic;
}
.workURL
{
font-size: 10px;
font-weight: bold;
padding-left: 20px;
}
/* The styles below are only necessary for the styling of the demo page: */
#main
{
position: relative;
margin: 0 auto;
width: 100%;
}
h1
{
padding: 30px 0;
text-align: center;
text-shadow: 0 1px 1px white;
margin-bottom: 30px;
background: url("../img/page_bg.gif") repeat-x bottom #f8f8f8;
}
h1, h2
{
font-family: "Myriad Pro" ,Arial,Helvetica,sans-serif;
}
h2
{
font-size: 14px;
font-weight: normal;
text-align: center;
position: absolute;
right: 40px;
top: 40px;
}
a, a:visited
{
color: #0196e3;
text-decoration: none;
outline: none;
}
a:hover
{
text-decoration: underline;
}
a img
{
border: none;
}
</style>
</head>
<body>
<header></header>
<nav></nav>
<!-- BEGIN CONTENT -->
<div id="main">
<!-- Content of portfolio -->
<div id="workListId" class="workList">
</div>
</div>
<!-- End div id main -->
<!-- END CONTENT -->
<footer></footer>
</body>
</html>
Related
Minipreview jQuery link preview contents of parent <p> or <div> only
I'd like to use https://github.com/lonekorean/mini-preview to create mouseover previews for parts of a website only. I have no problems using anchors from the target website to have the script render a full website preview, scrolled to where an individual anchor is. That's not what I'm after however. I'd like the script to show only the content of the anchors' parent <p> or <div>. On the site, the link target anchors are coded like this: <div class="paragraph"> <p> <a id="anchor_1"></a> Foo bar baz. </p> </div> So, I'd like the little preview box to show Foo bar baz. only. I suspect the answer lies in this part of the script: loadPreview: function() { this.$el.find('.' + PREFIX + '-frame') .attr('src', this.$el.attr('href')) .on('load', function() { // some sites don't set their background color $(this).css('background-color', '#fff'); }); specifically, the .attr('src', this.$el.attr('href')) part. I'm not sure though. Does anyone know how I can do this? Or can you recommend some other script that I can use to do this and makes things look as nice as this one? I'm not a web dev, so please go easy on me. Thanks UPDATE (based on Swati's answer and corresponding comments): For example, if my website includes this: <body> <p> See internal </p> <p> See external </p> <div class="paragraph"> <p> <a id="anchor_on_my_site"></a> Foo bar baz. </p> </div> </body> and the external website includes this: <body> <div class="paragraph"> <p> <a id="external_anchor"></a> Qux quux quuz. </p> </div> </body> I'd like See internal to display Foo bar baz. and See external to display Qux quux quuz.
Inside loadPreview function you can use closest('p').clone().children().remove().end().text() to get text from p tag where a has been hover then using this put that text to show inside your frame div i.e : .find('.' + PREFIX + '-frame').text(data_to_show) . Demo Code : (function($) { var PREFIX = 'mini-preview'; $.fn.miniPreview = function(options) { return this.each(function() { var $this = $(this); var miniPreview = $this.data(PREFIX); if (miniPreview) { miniPreview.destroy(); } miniPreview = new MiniPreview($this, options); miniPreview.generate(); $this.data(PREFIX, miniPreview); }); }; var MiniPreview = function($el, options) { this.$el = $el; this.$el.addClass(PREFIX + '-anchor'); this.options = $.extend({}, this.defaultOptions, options); this.counter = MiniPreview.prototype.sharedCounter++; }; MiniPreview.prototype = { sharedCounter: 0, defaultOptions: { width: 256, height: 144, scale: .25, prefetch: 'parenthover' }, generate: function() { this.createElements(); this.setPrefetch(); }, createElements: function() { var $wrapper = $('<div>', { class: PREFIX + '-wrapper' }); //no need to use iframe...use simple div var $frame = $('<div>', { class: PREFIX + '-frame' }); var $cover = $('<div>', { class: PREFIX + '-cover' }); $wrapper.appendTo(this.$el).append($frame, $cover); // sizing $wrapper.css({ width: this.options.width + 'px', height: this.options.height + 'px' }); // scaling var inversePercent = 100 / this.options.scale; $frame.css({ width: inversePercent + '%', height: inversePercent + '%', transform: 'scale(' + this.options.scale + ')' }); var fontSize = parseInt(this.$el.css('font-size').replace('px', ''), 10) var top = (this.$el.height() + fontSize) / 2; var left = (this.$el.width() - $wrapper.outerWidth()) / 2; //add more style here ...if needed to outer div $wrapper.css({ top: top + 'px', left: left + 'px', 'font-size': '55px', 'color': 'blue' }); }, setPrefetch: function() { switch (this.options.prefetch) { case 'pageload': this.loadPreview(); break; case 'parenthover': this.$el.parent().one(this.getNamespacedEvent('mouseenter'), this.loadPreview.bind(this)); break; case 'none': this.$el.one(this.getNamespacedEvent('mouseenter'), this.loadPreview.bind(this)); break; default: throw 'Prefetch setting not recognized: ' + this.options.prefetch; break; } }, loadPreview: function() { //to get text from p tag var data_to_show = this.$el.closest('p').clone().children().remove().end().text().trim() //set new text inside div frame this.$el.find('.' + PREFIX + '-frame').text(data_to_show) //set bg color.. this.$el.find('.' + PREFIX + '-frame').css('background-color', '#fff'); }, getNamespacedEvent: function(event) { return event + '.' + PREFIX + '_' + this.counter; }, destroy: function() { this.$el.removeClass(PREFIX + '-anchor'); this.$el.parent().off(this.getNamespacedEvent('mouseenter')); this.$el.off(this.getNamespacedEvent('mouseenter')); this.$el.find('.' + PREFIX + '-wrapper').remove(); } }; })(jQuery); $('a').miniPreview(); body { height: 100%; margin: 0; padding: 0 10% 40px; font-size: 2rem; line-height: 1.5; font-family: 'Roboto Slab', sans-serif; text-align: justify; color: #59513f; background-color: #f5ead4; } a { color: #537f7c; } .mini-preview-anchor { display: inline-block; position: relative; white-space: nowrap; } .mini-preview-wrapper { -moz-box-sizing: content-box; box-sizing: content-box; position: absolute; overflow: hidden; z-index: -1; opacity: 0; margin-top: -4px; border: solid 1px #000; box-shadow: 4px 4px 6px rgba(0, 0, 0, .3); transition: z-index steps(1) .3s, opacity .3s, margin-top .3s; } .mini-preview-anchor:hover .mini-preview-wrapper { z-index: 2; opacity: 1; margin-top: 6px; transition: opacity .3s, margin-top .3s; } .mini-preview-cover { background-color: rgba(0, 0, 0, 0); /* IE fix */ } .mini-preview-frame { border: none; -webkit-transform-origin: 0 0; transform-origin: 0 0; } <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <html> <body> <div class="paragraph"> <p> <a id="anchor_1">See</a> This is text we are showing for first </p> <p> <a id="anchor_2">See</a> This is text we are showing for second </p> </div> </body> </html> Update 1 : You can differentiate between external & internal link using some class i.e : simply check if the a tag which is hover has a particular class or not depending on this change your code to preview divs. Updated Code : if (this.$el.hasClass("internal")) { //to get text from p tag var data_to_show = this.$el.closest('p').siblings(".paragraph").clone().text().trim() //set new text inside div frame this.$el.find('.' + PREFIX + '-frame').text(data_to_show) //set bg color.. this.$el.find('.' + PREFIX + '-frame').css('background-color', '#fff'); } else { console.log("for external code ..") }
Is there a way to check the bottom of a child div?
I'm trying to implement an Infinity scroll. But not a window object, the target is a child div with a scroller. Is there a way to examine the current height of a child div with JavaScript? For example, I would like to request an event when the scroll touches at the end. This is my template code. <div style="overflow-y: scroll; height:500px;" class="scroll-content" #scroll="onScroll" >
Here is an example: var listElm = document.querySelector('#infinite-list'); // Add items. var nextItem = 1; var loadMore = function() { for (var i = 0; i < 10; i++) { var item = document.createElement('li'); item.innerText = 'Item ' + nextItem++; listElm.appendChild(item); } } // Detect when scrolled to bottom. listElm.addEventListener('scroll', function() { if (listElm.scrollTop + listElm.clientHeight >= listElm.scrollHeight) { loadMore(); } }); // Initially load some items. loadMore(); #infinite-list { /* We need to limit the height and show a scrollbar */ width: 200px; height: 100px; overflow: auto; /* Optional, only to check that it works with margin/padding */ margin: 30px; padding: 20px; border: 10px solid black; } /* Optional eye candy below: */ li { padding: 10px; list-style-type: none; } li:hover { background: #ccc; } <ul id='infinite-list'> </ul>
The following function returns, whether the user has scrolled to the bottom of a certain element: function scrollEnd(el) { return (el.scrollTop + el.offsetHeight >= el.scrollHeight); } If you add this to a scroll event listener: element.addEventListener('scroll', () => { if (scrollEnd(element)) { // the user reached the end } }) I tried this on a textarea, should work with anything, though.
Random name picker with bounce animation
I'm looking to create a random name picker with HTML, JS and CSS which has gone quite well as you can see here... http://clients.random.agency/namepicker/ However, the client has asked for it to have a similar animation to this with ... https://www.dropbox.com/s/3likecb0ld30som/Jv0Gp4XkhQ.mp4?dl=0 I've search google but I can't seem to find any examples of what I'm looking for and would really appreciate if anyone could point me in the right direction.
This is a simple example, hope be helpful. var names =['John', 'David', 'Joe', 'Sara']; var nameCount= names.length; var p = document.getElementById("container"); var randTimer = setInterval(function(){ p.innerHTML = names[Math.floor(Math.random() * nameCount)]; }, 200); function stop(){ clearInterval(randTimer); } #container{ color: red; font-size:2rem; text-align:center; cursor: pointer; } <p id="container" onClick="stop()"></p> <p>click on random names to pick one!</P>
Here's a pretty similar example I was able to find. Using Javascript seems to be the most straightforward way to go about doing this. https://codepen.io/maerianne/pen/pRQbQr var myScrollTop = function(elem, delay){ elem.animate({ scrollTop: 0 }, delay, function(){ myScrollBottom(elem, delay); }); }; var myScrollBottom = function(elem, delay){ elem.animate({ scrollTop: elem.height() }, delay, function(){ myScrollTop(elem, delay); }); }; var scrollUpDown = function(elem, delay) { myScrollTop(elem, delay); }; $(document).ready(function(){ scrollUpDown($(".scroll-up-down"), 5000); }); As you can see, scrollUpDown()is the initial function which starts a loop switching between myScrollTop() and myScrollBottom(). You could pretty easily make the delay increase with each iteration to mimic the slowing down and eventual stop in the example animation you gave. You could also refactor this to be a singular recursive function. Best of luck!
It picks a random item from the array of labels. Then it goes into a loop, changing the label to the next item in the array until it gets to the chosen one, and using animation for the transitions $('#search_btns button:nth-child(2)').hover(function() { btnTimeID = setTimeout(function() { // We are using the math object to randomly pick a number between 1 - 11, and then applying the formula (5n-3)5 to this number, which leaves us with a randomly selected number that is applied to the <ul> (i.e. -185) and corresponds to the position of a word (or <li> element, i.e. "I'm Feeling Curious"). var pos = -((Math.floor((Math.random() * 11) + 1)) * 5 - 3) * 5 if (pos === -135) { console.log("position didn't change, let's force change") pos = -35; } $('#search_btns button:nth-child(2) ul').animate({'bottom':pos + 'px'}, 300); // Change the width of the button to fit the currently selected word. if (pos === -35 || pos === -110 || pos === -185 || pos === -10 || pos === -60 || pos === -160) { console.log(pos + ' = -35, -110, -185, -10, -60, -160'); $('#search_btns button:nth-child(2)').css('width', '149px'); } else if (pos === -85) { console.log(pos + ' = -85'); $('#search_btns button:nth-child(2)').css('width', '160px'); } else if (pos === -210) { console.log(pos + ' = -210'); $('#search_btns button:nth-child(2)').css('width', '165px'); } else { console.log(pos + ' = -260, -235'); $('#search_btns button:nth-child(2)').css('width', '144px'); } },200); }, function() { clearTimeout(btnTimeID); setTimeout(function() { console.log('setTimeout function'); $('#search_btns button:nth-child(2) ul').css('bottom', '-135px'); // this is the original position $('#search_btns button:nth-child(2)').css('width', '144px'); // reset the original width of the button },200); }); body, html { margin: 0; box-sizing: border-box; font-family: arial; } *, *:before, *:after { box-sizing: inherit; } #search_btns { width: 400px; margin: 30px auto; padding-left: 60px; } #search_btns button:nth-child(2) { width: 144px; } #search_btns button:nth-child(1) { bottom: 12px; } #search_btns button { position: relative; height: 34px; margin: 3px; font-weight: bold; color: gray; background: #f1f1f1; border: 1px solid #f1f1f1; border-radius: 2px; padding: 0 15px; overflow: hidden; } #search_btns button:hover { color: black; border: 1px solid #bdbdbd; box-shadow: 0px 0.5px 0px 0px #d3d3d3; } #search_btns button:active { border: 1px solid #7f7fff; } #search_btns button:focus { outline: 0; } #search_btns button ul li { list-style-type: none; padding: 5px 0; text-align: left; } #search_btns button ul { padding-left: 0; position: absolute; bottom: -135px; width: 144px; } <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <div id="search_btns"> <button>This might be the effect you looking for</button> <button> <ul> <li>item0/li> <li>item1</li> <li>item2</li> <li>item3</li> <li>item4</li> <li>item5</li> <li>item6</li> <li>item7</li> <li>item8</li> <li>item9</li> </ul> </button> </div> </body> </html>
Generate Excel file thumbnail preview in Javascript
I have a project handling a library of excel files. To make it easilier for the users to visually scan them, I would like to generate preview thumbnail images of their content. Google drive does this (screenshot below) but I have no idea how. Any ideas/suggestions on how this could be done (without using the drive API) ?
I guess this is what you need http://github.com/lonekorean/mini-preview DEMO /* * MiniPreview v0.9 * * #author Will Boyd * #github http://github.com/lonekorean/mini-preview */ (function($) { var PREFIX = 'mini-preview'; // implemented as a jQuery plugin $.fn.miniPreview = function(options) { return this.each(function() { var $this = $(this); var miniPreview = $this.data(PREFIX); if (miniPreview) { miniPreview.destroy(); } miniPreview = new MiniPreview($this, options); miniPreview.generate(); $this.data(PREFIX, miniPreview); }); }; var MiniPreview = function($el, options) { this.$el = $el; this.$el.addClass(PREFIX + '-anchor'); this.options = $.extend({}, this.defaultOptions, options); this.counter = MiniPreview.prototype.sharedCounter++; }; MiniPreview.prototype = { sharedCounter: 0, defaultOptions: { width: 256, height: 144, scale: .25, prefetch: 'pageload' }, generate: function() { this.createElements(); this.setPrefetch(); }, createElements: function() { var $wrapper = $('<div>', { class: PREFIX + '-wrapper' }); var $loading = $('<div>', { class: PREFIX + '-loading' }); var $frame = $('<iframe>', { class: PREFIX + '-frame' }); var $cover = $('<div>', { class: PREFIX + '-cover' }); $wrapper.appendTo(this.$el).append($loading, $frame, $cover); // sizing $wrapper.css({ width: this.options.width + 'px', height: this.options.height + 'px' }); // scaling var inversePercent = 100 / this.options.scale; $frame.css({ width: inversePercent + '%', height: inversePercent + '%', transform: 'scale(' + this.options.scale + ')' }); // positioning var fontSize = parseInt(this.$el.css('font-size').replace('px', ''), 10) var top = (this.$el.height() + fontSize) / 2; var left = (this.$el.width() - $wrapper.outerWidth()) / 2; $wrapper.css({ top: top + 'px', left: left + 'px' }); }, setPrefetch: function() { switch (this.options.prefetch) { case 'pageload': this.loadPreview(); break; case 'parenthover': this.$el.parent().one(this.getNamespacedEvent('mouseenter'), this.loadPreview.bind(this)); break; case 'none': this.$el.one(this.getNamespacedEvent('mouseenter'), this.loadPreview.bind(this)); break; default: throw 'Prefetch setting not recognized: ' + this.options.prefetch; break; } }, loadPreview: function() { this.$el.find('.' + PREFIX + '-frame') .attr('src', this.$el.attr('href')) .on('load', function() { // some sites don't set their background color $(this).css('background-color', '#fff'); }); }, getNamespacedEvent: function(event) { return event + '.' + PREFIX + '_' + this.counter; }, destroy: function() { this.$el.removeClass(PREFIX + '-anchor'); this.$el.parent().off(this.getNamespacedEvent('mouseenter')); this.$el.off(this.getNamespacedEvent('mouseenter')); this.$el.find('.' + PREFIX + '-wrapper').remove(); } }; })(jQuery); .mini-preview-anchor { display: inline-block; position: relative; white-space: nowrap; } .mini-preview-wrapper { -moz-box-sizing: content-box; box-sizing: content-box; position: absolute; overflow: hidden; z-index: -1; opacity: 0; margin-top: -4px; border: solid 1px #000; box-shadow: 4px 4px 6px rgba(0, 0, 0, .3); transition: z-index steps(1) .3s, opacity .3s, margin-top .3s; } .mini-preview-anchor:hover .mini-preview-wrapper { z-index: 2; opacity: 1; margin-top: 6px; transition: opacity .3s, margin-top .3s; } .mini-preview-loading, .mini-preview-cover { position: absolute; top: 0; bottom: 0; right: 0; left: 0; } .mini-preview-loading { display: table; height: 100%; width: 100%; font-size: 1.25rem; text-align: center; color: #f5ead4; background-color: #59513f; } .mini-preview-loading::before { content: 'Loading...'; display: table-cell; text-align: center; vertical-align: middle; } .mini-preview-cover { background-color: rgba(0, 0, 0, 0); /* IE fix */ } .mini-preview-frame { border: none; -webkit-transform-origin: 0 0; transform-origin: 0 0; } <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>MiniPreview Demo</title> <link href="http://fonts.googleapis.com/css?family=Roboto+Slab" rel="stylesheet"> <style> body { height: 100%; margin: 0; padding: 0 10% 40px; font-size: 2rem; line-height: 1.5; font-family: 'Roboto Slab', sans-serif; text-align: justify; color: #59513f; background-color: #f5ead4; } a { color: #537f7c; } .break { text-align: center; } </style> <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> <!-- MiniPreview stuff here --> <link href="./jquery.minipreview.css" rel="stylesheet"> <script src="./jquery.minipreview.js"></script> <script> $(function() { $('#p1 a').miniPreview({ prefetch: 'pageload' }); $('#p2 a').miniPreview({ prefetch: 'parenthover' }); $('#p3 a').miniPreview({ prefetch: 'none' }); }); </script> </head> <body> <p id="p1"> This demo shows how to add live mini-previews to links on hover. Check out these links to SitePoint and A List Apart. Hover over them to see a small preview of what they point to. </p> <p class="break">• • •</p> <p id="p2"> Those previews were fetched as soon as this page loaded. This is great for having the previews ready ahead of time, but can eat up extra bandwidth. As an alternative, check out these links to Abduzeedo and Smashing Magazine. These previews aren't fetched until you hover over this paragraph. </p> <p class="break">• • •</p> <p id="p3"> Finally, check out these links to Daniel's blog, Joni's blog, and my blog. These previews are only fetched when needed. This saves the most bandwidth, but there will be a delay before the previews can be shown. </p> </body> </html> ORIGINAL SOURCE: http://codepen.io/kanakiyajay/pen/NqgZjo
I just use a library to generate a PNG preview of the excel file and show it. I use Free Spire.XLS for .NET because I'm in the .net world, but you can look at Wijmo Workbook Viewer for your Node.js needs.
Applying background color based on scrolling content
Here is my JsFiddle I want to apply background-color change property to circle when the window slides. Like in the beginning only first circle will have background-color. and when the images slides to second screen the second circle will have only color. Can anybody guide me how to achieve that. JQuery: $(document).ready(function () { setInterval(function () { var A = $('.gallery').scrollLeft(); if (A < 993) { $('.gallery').animate({ scrollLeft: '+=331px' }, 300); } if (A >= 993) { $('.gallery').delay(400).animate({ scrollLeft: 0 }, 300); } }, 3000); });
Here's a simple solution of your problem: http://jsfiddle.net/pjvCw/44/ but.... The way you're doing galleries is quite wrong. You have a really sensitive CSS full of margin bugs (see in CSS code), you calculate all by hand, which will just complicate your life one day if you'll get to add images, change widths etc... Your buttons are positioned really wrongly, and again you don't even need to manually add them in your HTML. Let jQuery do all the job for you: Calculate margins, widths, Get the number of slides generate buttons, Make your buttons clickable Pause gallery on mouseenter (loop again on mouseleave) LIVE DEMO This is the way you should go with your slider: HTML: <div class="galleryContainer"> <!-- Note this main 'wrapper' --> <div class="gallery"> <div class="row"> <!-- ..your images.. --> </div> <div class="row"> <!-- ..your images.. --> </div> </div> <div class="content-nav-control"></div> <!-- Let jQ create the buttons --> </div> Note the general gallery wrapper, it allows you with this CSS to make your buttons parent not move with the gallery. CSS: In your code, using display:inline-block; adds 4px margin to your elements, ruining your math. So you just need to apply font-size:0; to remove that inconvenience. As soon I did that the math was working and the right width was than 340px, having 5px border for your images and 20px margin. .galleryContainer{ /* you need that one // to prevent the navigation move */ position:relative; /* cause .content-nav-control is absolute */ background-color: #abcdef; width:340px; /* (instead of 350) now the math will work */ height: 265px; } .gallery{ position:relative; overflow: hidden; /* "overflow" is enough */ width:340px; /* (instead of 350) now the math will work */ height: 265px; } .gallery .row { white-space: nowrap; font-size:0; /* prevent inline-block 4px margin issue */ } .gallery img { display: inline-block; margin-left: 20px; margin-top: 20px; } .normalimage { height: 80px; width: 50px; border: 5px solid black; } .wideimage { height: 80px; width: 130px; border: 5px solid black; } img:last-of-type { margin-right:20px; } .content-nav-control { position: absolute; width:100%; /* cause it's absolute */ bottom:10px; text-align:center; /* cause of inline-block buttons inside*/ font-size:0; /* same trick as above */ } .content-nav-control > span { cursor:pointer; width: 20px; height: 20px; display: inline-block; border-radius: 50%; border:1px solid #000; box-shadow: inset 0 0 6px 2px rgba(0,0,0,.75); margin: 0 2px; /* BOTH MARGINS LEFT AND RIGHT */ } .content-nav-control > span.active{ background:blue; } And finally: $(function () { // DOM ready shorty var $gal = $('.gallery'), $nav = $('.content-nav-control'), galSW = $gal[0].scrollWidth, // scrollable width imgM = parseInt($gal.find('img').css('marginLeft'), 10), // 20px galW = $gal.width() - imgM, // - one Margin n = Math.round(galSW/galW), // n of slides c = 0, // counter galIntv; // the interval for(var i=0; i<n; i++){ $nav.append('<span />'); // Create circles } var $btn = $nav.find('span'); $btn.eq(c).addClass('active'); function anim(){ $btn.removeClass('active').eq(c).addClass('active'); $gal.stop().animate({scrollLeft: galW*c }, 400); } function loop(){ galIntv = setInterval(function(){ c = ++c%n; anim(); }, 3000); } loop(); // first start kick // MAKE BUTTONS CLICKABLE $nav.on('click', 'span', function(){ c = $(this).index(); anim(); }); // PAUSE ON GALLERY MOUSEENTER $gal.parent('.galleryContainer').hover(function( e ){ return e.type=='mouseenter' ? clearInterval(galIntv) : loop() ; }); }); "- With this solution, What can I do now and in the future? -" Nothing! just freely add images into your HTML and play, and never again have to take a look at your backyard :)
Try this: http://jsfiddle.net/yerdW/1/ I added a line that gets the scrollLeft and divides it by your width (331px) to get the position and use that to select the 'active' circle: $(".circle").removeClass("coloured"); position = Math.ceil($(".gallery").scrollLeft()/331 + 2); if(position > $(".circle").length){ position = 1; // yes... } $(".content-nav-control div:nth-child("+position+")").addClass("coloured"); Red background for active circle: .coloured { background : red; } Note that you should initialise with the first circle already having the .coloured class applied.
Here you go: http://jsfiddle.net/pjvCw/41/ i added new class .selected { background-color: red; } and modified some js code
Here is your jsfiddle edited http://jsfiddle.net/pjvCw/45/ var scrolled = 0; var circles = $(".circle"); var colorCircle = function(index) { for(var i=0; i<circles.length; i++) { if(i == index) { circles.eq(i).css("background-color", "rgba(255, 0, 0, 1)"); } else { circles.eq(i).css("background-color", "rgba(255, 0, 0, 0)"); } } } $(document).ready(function () { setInterval(function () { var A = $('.gallery').scrollLeft(); if (A < 993) { $('.gallery').animate({ scrollLeft: '+=331px' }, 300); colorCircle(++scrolled); } if (A >= 993) { $('.gallery').delay(400).animate({ scrollLeft: 0 }, 300); colorCircle(scrolled = 0); } }, 3000); colorCircle(0); }); I added a transition to the .circle class, so it looks a little bit better: .circle { width: 20px; height: 20px; display: inline-block; border-radius: 50%; border:1px solid #000; box-shadow: inset 0 0 6px 2px rgba(0,0,0,.75); margin-right: 5px; transition: background-color 700ms; -webkit-transition: background-color 700ms; }