How to tweak the flip javascript in this link http://desandro.github.io/3dtransforms/docs/card-flip.html so that it will flip to the third content or won't referse flip to the first content
var init = function() {
var card = document.getElementById('card');
document.getElementById('flip').addEventListener( 'click', function(){
card.toggleClassName('flipped');
}, false);
};
window.addEventListener('DOMContentLoaded', init, false);
The contents of each element should be stored separately in the HTML, and retrieved when needed.
<div class="container">
<div class="card">
<div class="face face1"></div>
<div class="face face2"></div>
</div>
<ul class="store">
<li>
<div class="content content1">1</div>
</li>
<li>
<div class="content content2">2</div>
</li>
<li>
<div class="content content3">3</div>
</li>
<li>
<div class="content content4">4</div>
</li>
</ul>
</div>
Answer copied and edited from
If I understand your question correctly, you only want the click event to happen once. If that's the case, and you're alright using jQuery, the jQuery 'one' function should be what you're looking for.
$( "#flip" ).one( "click", function() {
card.toggleClassName('flipped');
});
Related
I am learning js and jquery and have been trying to solve this litle issue I am having when trying to loop through a ul on the page and move a dom element specifically an image up two levels in my html. Here is what the html looks like:
<ul class="feedEkList">
<li class="slick-slide">
<div class="featured-title">
<div class="post-title">
some text
</div>
<div class="post-excerpt">
<img src="imageurl.jpg" class="wp-post-image">
<p>Post Description.</p>
</div>
</div>
</li>
</ul>
There are 10 li items in my ul but for readability purpose I only included one so you can see my html structure.
My javascript seems to be adding all the images before every featured-title div giving me piles of pictures instead of the one that is associated with that li.
$(function() {
$('.wp-post-image').insertBefore($('.featured-title'));
});
So then I tried this using each but it does not seem to do anything.
$(function() {
$('.feedEkList > li > .featured-title > .post-excerpt > .wp-post-image').each(function() {
$('.wp-post-image').insertBefore($('.featured-title'));
});
});
I was thinking maybe i need to use "this" in the code somewhere but I am new to javascript and not sure how to properly use "this" with the insert before, or if "this" is even the best option.
$('this.wp-post-image').insertBefore($('this.featured-title'));
Can anyone help me figure out what I am doing wrong? It is driving me nuts!
You need to iterate each image
For each image, get his parent .slick-slide.
Prepend the image to the .slick-slide using .prepend
Optional: If you need to insertBefore .featured-title see the comment.
$('.wp-post-image').each(function() {
var $this = $(this),
parentLi = $this.parents('.slick-slide').first();
$this.prependTo(parentLi);
//$this.insertBefore(parentLi.find('.featured-title'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="feedEkList">
<li class="slick-slide">
<div class="featured-title">
<div class="post-title">
some text
</div>
<div class="post-excerpt">
<img src="http://i.stack.imgur.com/FgiPG.jpg" class="wp-post-image" />
<p>Post Description.</p>
</div>
</div>
</li>
<li class="slick-slide">
<div class="featured-title">
<div class="post-title">
some text
</div>
<div class="post-excerpt">
<img src="http://i.stack.imgur.com/FgiPG.jpg" class="wp-post-image" />
<p>Post Description.</p>
</div>
</div>
</li>
<li class="slick-slide">
<div class="featured-title">
<div class="post-title">
removed the "wp-post-image" class to show how it was look before
</div>
<div class="post-excerpt">
<img src="http://i.stack.imgur.com/FgiPG.jpg" />
<p>Post Description.</p>
</div>
</div>
</li>
</ul>
Here is my problem (besides the fact that i'm a beginnen with javascript). I am building a page that wil load ajax content into a HTML page with a tab plugin. We need to build one page only which has to carry loads of content. Everything is working fine but we need to have little enhancement that uses javascript.
The first set are the tabs (ul.tabs) the second set are the content placeholders (div.panel-container) the third set is a layer popup (div#popup)
<div id="ajax-tab-container" class='tab-container'>
<ul class='tabs'>
<li class='tab'>one</li>
<li class='tab'>two</li>
<li class='tab'>three</li>
</ul>
<div class='panel-container'>
<div id="one" class="active"></div>
<div id="two"></div>
<div id="three"></div>
</div>
<div id="#popup">
<div class="one-ph">Content</div>
<div class="two-ph">Content</div>
<div class="three-ph">Content</div>
</div>
Now based on which tab is active the current scripts set the active status. Now what i need to do is create a script that checks (hasClass) which div within the .panel-container hasClass "active". But then it should addClass to the div within div#popup. But they have to match.
So if div#one hasClass "active" then addClass to div.one-ph,
if div#two hasClass "active" then addClass to div.two-ph etc.
Now in this example there are only three but the finished document will have about 20. kan somebody help me create this, i tried almost every example but is does not work. Also this is not a click function but a function that should work probably on document ready.
Please help this desperate guy
UPDATED
Fiddle
jQuery(document).ready(function() {
jQuery('.panel-container > div.active').each(function() {
var id = jQuery(this).attr('id');
var target = '.' + id + '-ph';
jQuery(target).addClass('active');
});
jQuery('ul.tabs > li.tab a').click(function(e) {
e.preventDefault();
var target = jQuery(this).attr('data-target');
var targetPopup = '.' + target.replace('#', '') + '-ph';
jQuery('.panel-container .active, #popup .active').removeClass('active');
jQuery(target).addClass('active');
jQuery(targetPopup).addClass('active');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="ajax-tab-container" class='tab-container'>
<ul class='tabs'>
<li class='tab'>one</li>
<li class='tab'>two</li>
<li class='tab'>three</li>
</ul>
<div class='panel-container'>
<div id="one" class="active"></div>
<div id="two"></div>
<div id="three"></div>
</div>
<div id="popup">
<div class="one-ph">Content</div>
<div class="two-ph">Content</div>
<div class="three-ph">Content</div>
</div>
</div>
Quick question. I've got html code like that:
<li class='entry'>
<div class='entryContent'>
<p class='entryText'>Entry text></p>
<a href="#" class='entryReply'>Reply</a>
</div>
</li>
When I click on Reply a div with reply form is being appended to 'entry'.
So it looks like:
<li class='entry'>
<div class='entryContent'>
<p class='entryText'>Entry text></p>
<a href="#" class='entryReply'>Reply</a>
</div>
<div class='replyForm'>
//form stuff
</div>
</li>
Now what I want to do is to create user-script which will find that dynamic div.replyForm and append some stuff into it:
<li class='entry'>
<div class='entryContent'>
<p class='entryText'>Entry text></p>
<a href="#" class='entryReply'>Reply</a>
</div>
<div class='replyForm'>
//form stuff
//MyAppendedStuff
</div>
</li>
I've already tried
$("body").on("focus", ".replyForm", function(){
$(this).append('<span>Hello World!</span>');
});
but it didn't work.
Any ideas?
You are creating a focus listener for div.replyForm. You probably want to bind the listener to the form inputs, instead. Something like this:
$('body').on("focus", "div.replyForm input", function () {
$('div.replyForm').append('<span>Hello World!</span>');
});
Working Example (jsfiddle)
I'm trying to have a one page solution with jQuery-One-Page-Nav for highlighting menu links when i reach some "sections" in the page (and click to scroll to respective sections).
Inside this sections i have some posts like the code
<body>
<nav>
<ul class="menuslides">
<li class="item-1">
<a href="#section-1" >One</a>
</li>
<li class="item-2">
<a href="#section-2" >Two</a>
</li>
...
</ul>
</nav>
<div id="wrapper">
<div id="wrap">
<section id="section-1">
<div class="post" id="item-slide-1">img</div>
<div class="post" id="item-slide-2">img</div>
</section>
<section id="section-2">
<div class="post" id="item-slide-3">img</div>
<div class="post" id="item-slide-4">img</div>
<div class="post" id="item-slide-5">img</div>
</section>
...
<section id="section-10">
<div class="post" id="item-slide-31">img</div>
...
</section>
</div>
<div id="buttons">
<div id="button" class="goto-prev">Prev</div>
<div id="button" class="goto-next">Next</div>
</div>
</div>
<script>
jQuery(document).ready(function() {
jQuery('.menuslides').onePageNav();
});
jQuery(function( $ ){
$('#wrapper').serialScroll({
target:'#wrap',
items:'.post',
prev:'div.goto-prev',
next:'div.goto-next',
axis:'y',
duration:700,
onBefore:function( e, elem, $pane, $items, pos ){
e.preventDefault();
if( this.blur )
this.blur();
},
onAfter:function( elem ){
}
});
});
</script>
</body>
I'm happy with the jQuery-One-Page-Nav, it works very well and i can navigate through "sections" with the nav menu.
But i need to add two button or two arrows that they work like a prev-next link scrolling the page through my posts class="post"
I'm using
https://github.com/davist11/jQuery-One-Page-Nav
and
https://github.com/flesler/jquery.serialScroll
with no weird changes to the code (only renamed ids and classes) but I can not get them coexist together: it works only one of them!
Is there any solution to make them co-exist, or is there a snippet of code that will enable me to have these two functions at the same time?
As this is my first post here,
I would like to thank all of you very much for your great support.
Now to the point...
I have 2 accordions in one page.
How can I prevent having 2 accordion tabs expanded?
I mean, if a tab from the first accordion is expanded, I want this tab to be collapsed, when click a tab from the second accordion.
Spent many hours searching over the net and Stack Overflow, but found nothing.
Example Code:
Javascript:
$(function() {
$( ".myAccordion" ).accordion({
collapsible: true,
active: -1
});
});
Html:
<div>Category One</div>
<div class="myAccordion">
<h3>Title1</h3>
<div>Text1</text>
<h3>Title2</h3>
<div>Text2</text>
</div>
<div>Category Two</div>
<div class="myAccordion">
<h3>Title1</h3>
<div>Text1</text>
<h3>Title2</h3>
<div>Text2</text>
</div>
I tried adding a changestart option:
changestart: function(event, ui) {
$('h3.ui-accordion-header').removeClass('ui-state-active ui-corner-top').addClass('ui-state-default ui-corner-all');
$('div.ui-accordion-content').slideUp();
$('div.ui-accordion-content').removeClass('ui-accordion-content-active ui-corner-top').addClass('ui-corner-all');
$('span.ui-icon').removeClass('ui-icon-triangle-1-s').addClass('ui-icon-triangle-1-e');
}
This does the trick, but only for the first time, a tab is clicked.
When you click that tab for a second time, it doesn't change at all. You have to click it for a third time to get it expand again.
Any help will be very appreciated.
Thanks again...
After I played a little and didn't find any solution, I decided to leave ui-accordion and make my own one! I believe that was the best solution to my problem!
As I didn't need fancy styling I used this code to achieve the result I needed:
<body>
<h1>Category 1</h1>
<div class="accTabs">
<a class="accLink" href="#">
<div class="tab-title">Tab Title</div>
<div class="tab-slide" style="display:none;">Tab Content to Slide</div>
</a>
</div>
<div class="accTabs">
<a class="accLink" href="#">
<div class="tab-title">Tab Title</div>
<div class="tab-slide" style="display:none;">Tab Content to Slide</div>
</a>
</div>
<h1>Category 2</h1>
<div class="accTabs">
<a class="accLink" href="#">
<div class="tab-title">Tab Title</div>
<div class="tab-slide" style="display:none;">Tab Content to Slide</div>
</a>
</div>
<div class="accTabs">
<a class="accLink" href="#">
<div class="tab-title">Tab Title</div>
<div class="tab-slide" style="display:none;">Tab Content to Slide</div>
</a>
</div>
<script>
$("a.accLink").click(function () {
var $that = $(this),
$children = $that.children('div.tab-slide');
// If expanded tab was clicked, collapse it.
// >> FIXED LOGIC ERROR >> if ($children.hasClass("slided")) {
if ($that.hasClass("slided")) {
$children.slideUp("slow", function () {
$that.removeClass("slided");
});
} else {
// Collapse all expanded tabs
// FIXED LOGIC ERROR >> $("div.slided").slideUp("slow", function() {
$(".slided").find("div.tab-slide").slideUp("slow", function () {
$(".slided").removeClass("slided");
});
// Expand clicked tab
$children.slideDown("slow", function () {
$that.addClass("slided");
});
}
});
</script>
</body>
UPDATED SCRIPT: FIXED LOGIC ERRORS TO MAKE 100% FUNCTIONAL
You want to attach your accordion() to a container element, not the actual divs.
For example:
<div id='accordion'>
<h3><a href='#'>Category One</a></h3>
<div class="myAccordion">
<h3>Title1</h3>
<div>Text1</text>
<h3>Title2</h3>
<div>Text2</text>
</div>
<h3><a href='#'>Category Two</a></h3>
<div class="myAccordion">
<h3>Title1</h3>
<div>Text1</text>
<h3>Title2</h3>
<div>Text2</text>
</div>
</div>
And then your script:
$(function() {
$( "#accordion" ).accordion({
collapsible: true,
active: false
});
});