So basically I'm working on a table that can dispose its rows with a nice animation. The problem is that I can't shrink a row beyond the point where its size is smaller than the size of its contents. Once the shrinking animation reaches the height of the text (I'm shrinking it vertically), it stops. Of course I looked for an answer on Google and Stack Overflow as well, and found quite a few, but I tried all of them with no success. I tried writing overflow: hidden; in the CSS, and all the other stuff that is claimed to solve this problem.
Here is my working example (sorry for not showing the code here, but it's easier to just link a working and editable example). Click the button to create a new table, then click the rows to make them go away.
I know this question has been answered elsewhere before, but those solutions don't seem to work for me.
Any ideas? I want the clicked row to shrink completely before getting deleted.
PS.: sorry if I'm being dumb, I'm new to web development.
PS2.: no jQuery please.
You have to update / change the lineheight like this
https://jsfiddle.net/9hznp00s/12/
function deleteRow(id) {
if (typeof (id) === 'undefined') return;
var element = document.getElementById(id);
// Set the row's height and opacity to 0 (the changes are animated by the CSS)
// I commented this out so it's easier to see the collapse effect
//element.style.opacity = 0;
element.style.height = '0px';
element.style.maxHeight = '0px';
element.style.lineHeight = '0px';
element.style.opacity=0.0;
// After the animation is done, remove the row form the HTML
setTimeout(function () {
element.innerHTML = '';
element.parentNode.removeChild(element);
}, 500);
}
or try one of these
https://jsfiddle.net/9hznp00s/8/
function deleteRow(id) {
if (typeof (id) === 'undefined') return;
var element = document.getElementById(id);
// Set the row's height and opacity to 0 (the changes are animated by the CSS)
// I commented this out so it's easier to see the collapse effect
//element.style.opacity = 0;
element.style.height = '0px';
element.style.maxHeight = '0px';
element.style.fontSize = '0px';
// After the animation is done, remove the row form the HTML
setTimeout(function () {
element.innerHTML = '';
element.parentNode.removeChild(element);
}, 500);
}
https://jsfiddle.net/9hznp00s/10/
function deleteRow(id) {
if (typeof (id) === 'undefined') return;
var element = document.getElementById(id);
// Set the row's height and opacity to 0 (the changes are animated by the CSS)
// I commented this out so it's easier to see the collapse effect
//element.style.opacity = 0;
element.style.height = '0px';
element.style.maxHeight = '0px';
element.style.fontSize = '0px';
element.style.opacity=0.0;
// After the animation is done, remove the row form the HTML
setTimeout(function () {
element.innerHTML = '';
element.parentNode.removeChild(element);
}, 500);
}
Related
Background
I am trying to create an infinite scrolling table inside a fixed position div. The problem is that all the solutions I come across use the window height and document scrollTop to calculate if the user has scrolled to the bottom of the screen.
Problem
I have tried to create a jQuery plugin that can calculate if a user has scrolled to the bottom of a fixed div with overflow: scroll; set.
My approach has been to create a wrapper div (the div with a fixed position and overflow: scroll) that wraps the table, I also place another div at the bottom of the table. I then try calculate if the wrapper.scrollTop() is greater than the bottom div position.top every time the wrapper is scrolled. I then load the new records and append them to the table body.
$.fn.isScrolledTo = function () {
var element = $(this);
var bottom = element.find('.bottom');
$(element).scroll(function () {
if (element.scrollTop() >= bottom.position().top) {
var tableBody = element.find("tbody");
tableBody.append(tableBody.html());
}
});
};
$('.fixed').isScrolledTo();
See Example http://jsfiddle.net/leviputna/v4q3a/
Question
Clearly my current example is not correct. My question is how to I detect when a user has scrolled to the bottom of a fixed div with overflow:scroll set?
Using the bottom element is a bit clunky, I think. Instead, why not use the scrollHeight and height to test once the scrollable area has run out.
$.fn.isScrolledTo = function () {
var element = this,
tableBody = this.find("tbody");
element.scroll(function(){
if( element.scrollTop() >= element[0].scrollHeight-element.height()){
tableBody.append(tableBody.html());
}
});
};
$('.fixed').isScrolledTo();
EDIT (12/30/14):
A DRYer version of the plugin might be much more re-usable:
$.fn.whenScrolledToBottom = function (cback_fxn) {
this.on('scroll',this,function(){
if( ev.data.scrollTop() >= ev.data[0].scrollHeight - ev.data.height()){
return cback_fxn.apply(ev.data, arguments)
}
});
};
Plugin Usage:
var $fixed = $('.fixed'),
$tableBody = $fixed.find("tbody");
$fixed.whenScrolledToBottom(function(){
// Load more data..
$tableBody.append($tableBody.html());
});
I have modified your code to handle the scroll event with a timer threshold:
$.fn.isScrolledTo = function () {
var element = $(this);
var bottom = element.find('.bottom');
$(element).scroll(function(){
if (this.timer) clearTimeout(this.timer);
this.timer=setTimeout(function(){
if( element.scrollTop() >= bottom.position().top){
var tableBody = element.find("tbody");
tableBody.append(tableBody.html());
}
},300);
});
};
$('.fixed').isScrolledTo();
The issue you are having is that as you scroll, new scroll event is being generated. Your code might have other issues, but this is a start.
I'm making a site that has an area that it's content disappear and re-appears. So when the user clicks certain button, the <div>'s content fades out and fades in the content relative to the clicked icon.
First time the function getabout is clicked it works OK, but whenever I click on clear() and then again on getabout it starts blinking. I've discovered that it does the clean to the div but it happens that the content re-appears again from nothing and becomes intermittent.
Here is my JavaScript code, it's commented so you could give me a hand here:
var check = null; //this will be checking the instance of div's content
const wait_time = 50; //the time it will take to fade
function getabout(id) {
/* prevent second call to the same function to bug */
if (check == id) return;
var titleOpacity = 0,
textOpacity = 0;
/* this changes the title first */
document.getElementById("title").style.opacity = 0;
document.getElementById("title").innerHTML = "this is the title";
// recursive call to the opacity changer, it
// increases opacity by 0.1 each time until it's 1
setInterval(function () {
titleOpacity = fadeIn(titleOpacity, 'title');
}, wait_time);
/* changes the content next to the title */
window.setTimeout(function () {
document.getElementById("dialog").style.opacity = 0;
document.getElementById("dialog").innerHTML = "this is the content";
setInterval(function () {
textOpacity = fadeIn(textOpacity, 'dialog');
}, wait_time);
}, 500);
check = id; // defines the instance "about" at the moment
}
function fadeIn(opacity, id) {
opacity += 0.1;
document.getElementById(id).style.opacity = opacity;
document.getElementById(id).style.MozOpacity = opacity;
if (opacity >= 1.0) clearInterval(listener);
return opacity;
}
function clear() {
var opacity = document.getElementById("title").style.opacity;
// supposed to decrease the opacity by 0.1 but it's not doing that
setInterval(function () {
opacity = fadeout(opacity);
}, wait_time);
//cleans the title and dialog to fill with the next button user clicked
document.getElementById("title").innerHTML = "";
document.getElementById("dialog").innerHTML = "";
}
function fadeout(opacity) {
opacity -= 0.1;
document.getElementById("title").style.opacity = opacity;
document.getElementById("dialog").style.MozOpacity = opacity;
if (opacity <= 0.0) clearInterval(listener);
return opacity;
}
function getregister(id) {
if (check == id) return;
clear(); // proceed to fade out the content and clean it
check = id;
}
I don't understand what is the error of the code. with the clear() function it should smoothly fade out the content and then clean it. But it just cleans the div. And next time I use getabout() function, instead of smoothly fade in again as it does the first time, it starts to blink.
I'm relatively new to web programming and I refuse JQuery for now. I want to understand deeply javascript before go to JQuery and this is why I would just like to know pure JavaScript solutions and considerations about this.
Ive managed to cock up my comment so trying again!
I think your problem is that you're not clearing the setInterval correctly - ensure you use listener = setInterval(...)
As it stands your clearInterval(listener); is doing nothing as 'listener' is not defined. So your fade out function continues to run.
Can someone help me with JavaScript code that resizes a font in a div if the screen width is lower than 1100px
if (window.screen.width <= 1100) {
var item = document.getElementById("div1");
item.style.fontSize = "25px";
item.innerHTML = "String";
}
This is what I have so far. Can someone help me with what to do next?
Your code works for me in JS Fiddle. Perhaps you are not specifying the correct id for your div or something like that.
http://jsfiddle.net/trott/GqFPY/
If you are hoping the code will be triggered on a browser resize, you will need to bind it to an event. (See Michael's answer.)
You will need to bind the action to the window.onresize event:
var resizeFonts = function() {
var item = document.getElementById("div1");
if (window.screen.width <= 1100) {
item.style.fontSize = "25px";
item.innerHTML = "String";
}
// Otherwise set a larger font
else item.style.fontSize = "30px";
};
window.onload = resizeFonts;
window.onresize = resizeFonts;
I have an OLD blog in which I posted about changing the font size, but it was made to show how to use jQueryUI slider. Maybe you can use some of the logic there to create your own solution:
http://weblogs.asp.net/thiagosantos/archive/2009/03/21/my-first-time-with-jquery-ui.aspx
I am looking for some native JavaScript, or jQuery plugin, that meets the following specification.
Sequentially moves over a set of images (ul/li)
Continuous movement, not paging
Appears infinite, seamlessly restarts at beginning
Ability to pause on hover
Requires no or minimal additional plugins
I realize this sounds simple enough. But I have looked over the web and tried Cycle and jCarousel/Lite with no luck. I feel like one should exist and wanted to pose the question before writing my own.
Any direction is appreciated. Thanks.
you should check out Nivo Slider, I think with the right configuration you can it to do what you want.
You can do that with the jQuery roundabout plugin.
http://fredhq.com/projects/roundabout/
It might require another plugin.
Both answers by MoDFoX and GSto are good. Usually I would use one of these, but these plugins didn't meet the all the requirements. In the end this was pretty basic, so I just wrote my own. I have included the JavaScript below. Essentially it clones an element on the page, presumably a ul and appends it to the parent container. This in effect allows for continuous scrolling, right to left, by moving the element left and then appending it once out of view. Of course you may need to tweak this code depending on your CSS.
// global to store interval reference
var slider_interval = null;
var slider_width = 0;
var overflow = 0;
prepare_slider = function() {
var container = $('.sliderGallery');
if (container.length == 0) {
// no gallery
return false;
}
// add hover event to pause slider
container.hover(function() {clearInterval(slider_interval);}, function() {slider_interval = setInterval("slideleft()", 30);});
// set container styles since we are absolutely positioning elements
var ul = container.children('ul');
container.css('height', ul.outerHeight(true) + 'px');
container.css('overflow', 'hidden')
// set width and overflow of slider
slider_width = ul.width();
overflow = -1 * (slider_width + 10);
// set first slider attributes
ul.attr('id', 'slider1');
ul.css({"position": "absolute", "left": 0, "top": 0});
// clone second slider
var ul_copy = ul.clone();
// set second slider attributes
ul.attr('id', 'slider2');
ul_copy.css("left", slider_width + "px");
container.append(ul_copy);
// start time interval
slider_interval = setInterval("slideleft()", 30);
}
function slideleft() {
var copyspeed = 1;
var slider1 = $('#slider1');
var slider2 = $('#slider2');
slider1_position = parseInt(slider1.css('left'));
slider2_position = parseInt(slider2.css('left'));
// cross fade the sliders
if (slider1_position > overflow) {
slider1.css("left", (slider1_position - copyspeed) + "px");
}
else {
slider1.css("left", (slider2_position + slider_width) + "px");
}
if (slider2_position > overflow) {
slider2.css("left", (slider2_position - copyspeed) + "px");
}
else {
slider2.css("left", (slider1_position + slider_width) + "px");
}
}
to debug some javascript code, I am looking for javascript code (preferably just js, without libraries and dependencies) that can highlight a div or span (probably by putting over it a div or span of the same size and shape with a bright color and some transparency).
I pretty sure it can be done, but I don't know how to start.
CLARIFICATION
I need to put a semi transparent div on top of my element. Modifying the background or adding borders will not help as my elements have themselves backgrounds and borders.
element.style.backgroundColor = "#FDFF47";
#FDFF47 is a nice shade of yellow that seems perfect for highlighting.
Edit for clarification: You're over-complicating things. If you ever want to restore the previous background color, just store element.style.backgroundColor and access it later.
If you're debugging in a browser that supports the CSS outline, one simple solution is this:
myElement.style.outline = '#f00 solid 2px';
If for some reason you need to use javascript here is function that temporary highlits element background
function highlight(element) {
let defaultBG = element.style.backgroundColor;
let defaultTransition = element.style.transition;
element.style.transition = "background 1s";
element.style.backgroundColor = "#FDFF47";
setTimeout(function()
{
element.style.backgroundColor = defaultBG;
setTimeout(function() {
element.style.transition = defaultTransition;
}, 1000);
}, 1000);
}
Old post, but worth adding since it shows up in searches on the topic. A simple way to achieve a highlighting effect is:
myElement.style.filter = "brightness(125%)";
function highlight(element) {
var div = highlight.div; // only highlight one element per page
if(element === null) { // remove highlight via `highlight(null)`
if(div.parentNode) div.parentNode.removeChild(div);
return;
}
var width = element.offsetWidth,
height = element.offsetHeight;
div.style.width = width + 'px';
div.style.height = height + 'px';
element.offsetParent.appendChild(div);
div.style.left = element.offsetLeft + (width - div.offsetWidth) / 2 + 'px';
div.style.top = element.offsetTop + (height - div.offsetHeight) / 2 + 'px';
}
highlight.div = document.createElement('div');
// set highlight styles
with(highlight.div.style) {
position = 'absolute';
border = '5px solid red';
}
Do you use Firebug? It makes it very simple to identify dom elements and will highlight them in the page as you walk through the dom.
Here is a function that combines the top 2 answers:
function highlight(element){
let defaultBG = element.style.backgroundColor;
let defaultOutline = element.style.outline;
element.style.backgroundColor = "#FDFF47";
element.style.outline = '#f00 solid 4px';
setTimeout(function()
{
element.style.backgroundColor = defaultBG;
element.style.outline = defaultOutline;
}, 2000);
}