CSS transition for multiple elements on the page - html

On my page I have 2 divs:
<div class="mycards fight">
<div class="card"><span class="name"></span><div id="att1"><span></span></div><div id="def1"><span></span></div></div>
</div>
<div class="mycards fight2">
<div class="card"><span class="name"></span><div id="att2"><span></span></div><div id="def2"><span></span></div></div>
</div>
I am injecting dome text via jquery like:
$('#att1').html(sometext); $('#att2').html(sometext); etc.
In CSS I set the transition for these elements:
#att1, #att2, #def1, #def2 {
transition: all 1s ease;
}
The problem is, the transition is working only for second DIV, where #att2 and #def2 are present and it ignores it in my first DIV.
I also tried to do it via jquery :
$("#att1").addClass('trans');$("#def1").addClass('trans');
$("#att2").addClass('trans');$("#def2").addClass('trans');
in my script when it is needed, and then in CSS I defined just
.trans {
transition: all 1s ease;
}
but again, it works now only for the first DIV.
For some reason it can't do the same effect for both elemets. Not sure where is the problem.
Example where the transition is expected:
function bonus(val,val1,val2) {
$.ajax
({url: 'bonus.php',
data: {"var1": val,"var2": val1,"var3": val2},
type: 'get',
dataType: 'json',
success: function(datax) {
if (val2===1) {
if (val===1) {$("#att1").addClass('size-50');}
if (val===2) {$("#def1").addClass('size-50');}
if (val===3) {$("#att1").addClass('size-50');$("#def1").addClass('size-50');}
window.setTimeout(function(){
if (val===1) {$("#att1").removeClass('size-50'); $("#att1").addClass('size-30');}
if (val===2) { $("#def1").removeClass('size-50'); $("#def1").addClass('size-30');}
if (val===3) {$("#att1").removeClass('size-50'); $("#att1").addClass('size-30');
$("#def1").removeClass('size-50'); $("#def1").addClass('size-30');}
},500);
}
else {
if (val===1) {$("#att2").addClass('size-50')}
if (val===2) {$("#def2").addClass('size-50');}
if (val===3) {$("#att2").addClass('size-50');$("#def2").addClass('size-50');}
window.setTimeout(function(){
if (val===1) {$("#att2").removeClass('size-50'); $("#att2").addClass('size-30');}
if (val===2) { $("#def2").removeClass('size-50'); $("#def2").addClass('size-30');}
if (val===3) {$("#att2").removeClass('size-50'); $("#att2").addClass('size-30');
$("#def2").removeClass('size-50'); $("#def2").addClass('size-30');}
},500);
}
window.setTimeout(function(){runAll();},1000);
}});
}

Damn, I think the issue was in the conditions I had there using e.g. val===1 which wrongly compared the values. when I replaced it for == now it works for both cases.
Sometimes the core issue is caused by something else...

Related

Opacity transition on page load does not kicks in on load

I want to have a very simple loading effect with a quick opacity change on the whole body. So I use the following CSS
body {
opacity: 0;
transition: opacity 2s;
}
body.show-page {
opacity: 1;
}
and add .show-page on load. Here is the live code http://plnkr.co/edit/Ze5TiqkZYiM41VJZVDuB?p=preview
For some reason it does not transition. After the page is loaded the transition works if I add/remove this class, but when loading, it does not happen. Any idea why?
Your script will run before body has finished loading as the script is inside the body tag.
You maybe want something like this:
function fadeIn() {
var body = document.getElementsByTagName('body')[0];
body.className += 'show-page';
}
window.onload = fadeIn;
Updated Plunker
One workaround can be to slightly delay (50ms) your JS doing so:
setTimeout(function(){body.className += 'show-page';}, 50);

How to set div to expand with transition on text change

I want to seamlessly expand my div (in a non-jarring way) when the text inside it changes:
The CSS transition: all 2s ease; is working great for colour changes, manually setting width, etc (as you can try out in the jsfiddle - click button to toggle width). but when the inner text of the div is changed the div just jumps to the new width without any transition.
How can I get the transition working when the inner text changes?
Thanks!
Because the default width of the div will be auto (100%), it can't transition from auto to a numerical value.
I don't think dynamically changing a width of an element depending on its content is possible, as there is no transition for content. The content of an element changes instantly and the width does not get a numerical value in your case - but adjusts.
A solution that can be sometimes applicable: Using a function to roughly calculate your text's width so that you'll be able to set a numerical width for your element on change.
Here's a simple one that I made.
function getTextWidth(text, css) {
var e = $('<span></span>'); // dummy element
e.css(css); // set properties
e.text(text); // set test
var width = e.appendTo($('body')).width(); // append and get width
e.remove(); // remove from DOM
return width;
}
Put together an example of usage.
Hi abagshaw try this script to solved your problem.
var big = false;
$('#content').on('click', function(e) {
if(!big)
{
$( this).animate({
width: "600px" }, 500 );
this.innerHTML = "MORETEXTMORETEXTMORETEXTMORETEXTMORETEXTMORETEXTMORETEXT";
big = true;
}
else
{
$( this).animate({
width: "200px" }, 500 );
this.innerHTML = "LESSTEXTLESSTEXT";
big = false;
}
});
.ui.transitioning.button{
transition:all 0.5s ease-in-out;-webkit-transition:all 0.5s ease-in-out;
width:200px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.0.7/semantic.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="ui transitioning teal button" id="content">LESSTEXTLESSTEXT</div>

Grey page content when side menu is opened

If you look at all the Google apps when you open the side (hamburger) menu the content of the app is greyed.
Here is an example
Is it possible to do this with ion-side-menu in ionic framework? If so, how?
Thank you.
Based on Mark Veenstra's answer, here is the result I came with.
In CSS:
.opaque-content {
opacity: .5;
transition: opacity 300ms ease-in-out;
}
In the controller I'm watching the open ratio of the side menu and set a flag:
$scope.$watch(
function () {
return $ionicSideMenuDelegate.getOpenRatio();
},
function (ratio) {
$scope.sidemenuopened = (ratio == 1);
});
In the template I'm using ng-class to conditionally apply the class:
<ion-side-menus>
<ion-side-menu-content ng-class="{'opaque-content' : sidemenuopened}">
<ion-nav-bar>
</ion-nav-bar>
</ion-side-menu-content>
</ion-side-menus>
This works and makes the page content partially transparent when the side menu is opened.
I believe this is not standard available in Ionic, but if you look at the $ionicModal you can see they do use the same technique there.
If you look at the CSS they use for this option, you should add the following to the correct class:
opacity: .5;
transition: opacity 300ms ease-in-out;
background-color: #000;
You should somehow detect when the side menu is exposed and then apply above CSS to the <ion-nav-view>.
I think you could create a directive or so which watches the $ionicSideMenuDelegate.isOpen() function and based on result apply or remove the CSS.
You only need CSS for this.
When the side menu is opened, there is a CSS class menu-open added to the body tag.
So just add the following and you will get what you want.
body.menu-open ion-side-menu-content {
-webkit-transition: opacity 300ms ease-in-out;
transition: opacity 300ms ease-in-out;
opacity: 0.5;
}
Based on Marius Bancila's answer, here is my solution.
In CSS: nothing
In the controller I'm watching the open ratio of the side menu and set a flag:
$scope.$watch(
function () {
return $ionicSideMenuDelegate.getOpenRatio();
},
function (ratio) {
$scope.sidemenuopened = (ratio == 1);
});
In the template I used modal backdrop background class instead of your opaque-content which isn't gray at all:
<ion-side-menus>
<ion-side-menu-content>
<div ng-class="{'modal-backdrop-bg' : sidemenuopened}"></div>
<ion-nav-bar>
</ion-nav-bar>
</ion-side-menu-content>
</ion-side-menus>
With this you will have the same effect as Google!

Enable last div of same class

I am making an ajax call to get more results and get them appended to the existing ones in the same page. While the contents are fetched, i need to show a scrolling spinner every time requests are send. In order to achieve this i go on adding a div at bottom of results fetched like this:
<div class="loader" style="display:block"></div>
And i am trying to show the spinner using the below code:
beforeSend: function()
{
$('#loader:last-child').html('<img src="/graphics/loading.gif" alt="Wait" />');
}
complete: function()
{
$('#loader:last-child').html('');
}
But i am not able to get the spinner effect, it is not at all shown on the page(results are though fetched).
Please help me in getting it corrected. Any other thoughts to have the spinner effect are also appreciated.
EDIT: I have removed quotes as they wre in my perl code. sorry for that.
Put a space in <imgsrc so that its <img src
Your selector should be $('.loader:last-child') since loader is a class not an id. (Use . instead of #)
Instead of adding any new elements or images, you could just add a class to the container of your results while it's loading, and use the class to add padding and the spinner graphic as the background image of the container. When the loading completes, remove the class:
beforeSend: function(){
$('#resultsContainer').addClass('loading');
}
complete: function(){
$('#resultsContainer').removeClass('loading');
}
Then your CSS could look like:
#resultsContainer.loading {
background: url(graphics/loading.gif) no-repeat center bottom;
padding-bottom: 2em;
}
For extra style, add transitions to padding-bottom on resultContainer, so that the loading graphic slides smoothly in and out.
Try this
beforeSend: function()
{
$("#loader").last().html('<img src="/graphics/loading.gif" alt="Wait" />');
}
complete: function()
{
$("#loader").last().html('');
}
why are you using \ slashes
try this
beforeSend: function()
{
$('.loader:last-child').html('<img src="/graphics/loading.gif" alt="Wait" />');
}
complete: function()
{
$('.loader:last-child').html('');
}
you have to use . for selecting class not # its for id and also , is missing between beforeComplete and complete.
You do not need to escape the quotes, also put a space between imgsrc like img src
As well mentioned by rob in his answer that
Your selector should be $('.loader:last-child') since loader is a class not an id. Use pereiod (.) instead of (#)
Also add a comma , after beforeSend: function(){}, and try;
Use the following code:
beforeSend: function() {
$('.loader:last-child').html('<img src="/graphics/loading.gif" alt="Wait" />');
},
complete: function() {
$('.loader:last-child').html('');
}
Try appending the loader image to the HTML of e #loader and then remove it.
(note that I added an id to the image)
beforeSend: function()
{
      $('.loader').append('<img src="/graphics/loading.gif" id="waitSpinner" alt="Wait" />');
}
complete: function()
{
      $('#waitSpinner').remove();
}
Edit: changed #loader to .loader, as that was the class.

Expand/Collapse Div

I am trying to create two buttons, one that will expand a div and one that will collapse it. I've tried to modify this code, but I cant seem to get it to work. I think I just dont understand how to not toggle a link.
I am also trying to make the DIV appear when the page loads. I'm not even sure if this is possible with the way I am writing the code.
Can anyone help me understand what I need to do to get this to work.
http://jsfiddle.net/XUjAH/93/
I am trying to avoid using .slideUp/.slideDown it seems to be interfering with another plugin I am using on the page.
$('#click-meopen').click(function() {
var height = $("#this").height();
if( height > 0 ) {
$('#this').css('height','0');
} else {
$("#this").css({'position':'absolute','visibility':'hidden','height':'auto'});
var newHeight = $("#this").height();
$("#this").css({'position':'static','visibility':'visible','height':'0'});
$('#this').css('height',newHeight + 'px');
}
});
$('#click-meclose').click(function() {
var height = $("#this").height();
if( height > 0 ) {
$('#this').css('height','0');
} else {
$("#this").css({'position':'absolute','visibility':'hidden','height':'auto'});
var newHeight = $("#this").height();
$("#this").css({'position':'static','visibility':'visible','height':'0'});
$('#this').css('height',newHeight + 'px');
}
});
All following text is just IMHO :)
See my exmple here - http://jsfiddle.net/XUjAH/99/
html:
<p id="click-meopen">click me to open</p>
<p id="click-meclose">click me to close</p>
<div id="this">
<div id="content">here<br />is<br />a<br />bunch<br />of<br />content<br />sdf</div>
</div>
as for JS:
$('#click-meopen').click(function() {
$("#this").height($("#content").height());
});
$('#click-meopen').click();
$('#click-meclose').click(function() {
$("#this").height('0');
});​
as for CSS it should be the same you have.
UPD: Seems that animation is a bit flaky - when you set 'height: auto' - div is visible from the beginning, however close button ignores animation on first click(I have Chrome with latest update) so I've added some workaround. Also added other styles to support this animation for other browsers like Firefox and Opera and not only for those who support -webkit.
http://jsfiddle.net/XUjAH/110/
in CSS added class and removed 'transition' from the main style:
.with-animation {
-webkit-transition: height 1s ease-in-out;
-moz-transition: height 1s ease-in-out;
-o-transition: height 1s ease-in-out;
transition: height 1s ease-in-out;
}
In JS:
// However our div already have proper size this line
// prevents instantaneous close on first click, don't know why
$("#container").height($("#content").height());
$('#click-meopen').click(function() {
$("#container").height($("#content").height());
});
$('#click-meclose').click(function() {
// If we add class with-animation on upper level div will
// start animation on page load
$("#container").height('0').addClass("with-animation");
});
​
If you can use toggle then all you need is $("#this").toggle('slow');, but you must replace height: 0; with display: none;
$('#click-meopen').click(function() {
$('#this').show();
});
$('#click-meclose').click(function() {
$('#this').hide();
});​
http://jsfiddle.net/XUjAH/95/
Or with toggle
$('#click-meopen').click(function() {
$('#this').toggle();
});
http://jsfiddle.net/XUjAH/96/
You are trying to change this to buttons ?
<input type="submit" id="click-meopen" value="open"></input>
<input type="submit" id="click-meclose" value="close"></input>
<div id="this">here<br />is<br />a<br />bunch<br />of<br />content<br />sdf</div>
That's not hard to do.
I would also recommend you to use .toggle() or the .show(),.hide() methods of jQuery. Just setting the css height property to zero is not a good practice.
Do you want to show the div when the page is loading or when its finished loading ( the DOM is ready) ?
Your JavaScript code would be:
$('#click-meopen').click(function() {
$('#this').show(); // or .toggle()
});
$('#click-meclose').click(function() {
$('#this').hide(); // or .toggle()
});​
If you would use only one button you should use the .toggle() method, otherwise stick with the .show(),.hide() method !
When the DOM is ready ( The Page is loaded completely):
$(document).ready(function(){
$('#this').show();
});
documentation of the methods that are used:
http://api.jquery.com/show/
http://api.jquery.com/hide/
http://api.jquery.com/toggle-event/
http://api.jquery.com/ready/
I see that you have used -webkit-transition which works for me in chrome but i guess you want it to work in other browses than webkit.
There are two ways i can suggest, one is to just simply add -moz-transition: height 1s ease-in-out; and transition: height 1s ease-in-out; to do it with css3 but that wont help in older ie browsers.
An other way is to place a div inside your text in the div you want to collapse:
<div id="this">
<div id="height">
text
text
</div>
</div>
then in your javascript you can get the height of the inner div-element, and in that way you can use animate instead of height: auto.
$('#click-meopen').click(function() {
$('#this').animate({'height': $('#height').height()}, 1000);
});
$('#click-meclose').click(function() {
$('#this').animate({'height': 0}, 1000);
});
that should be enough for you.
​
$('#click-meopen').click(function() {
$("#this").css({'position':'absolute','visibility':'hidden','height':'auto'});
var newHeight = $("#this").height();
$("#this").css({'position':'static','visibility':'visible','height':'0'});
$('#this').css('height',newHeight + 'px');
});
$('#click-meclose').click(function() {
$('#this').css('height','0');
});
$('#click-meopen').click();
http://jsfiddle.net/XUjAH/100/