In This theme object i have created 2 properties.
I placed this.changeThemeTo(1); under the Event Listener, after that it worked. But i want to place it within if tag
But seems giving an error when put it within if
Uncaught TypeError: this.changeThemeTo is not a function
please help to fix this. Thanks..
var theme = {
changeThemeTo: function (theme_value) {
sessionStorage.removeItem('THEME'); // remove old theme from session storage
if (theme_value == 0) {
sessionStorage.setItem("THEME", 'dark');
} else if (theme_value == 1) {
sessionStorage.setItem("THEME", 'light');
}
document.body.className = sessionStorage.getItem("THEME");
},
init: function () {
document.body.classList.add("fade");
setTimeout(function () {
document.body.classList.remove("fade");
}, 100);
var themes = ['dark', 'light'];
themes.forEach(function (item) {
var button = document.querySelector("." + item);
if (button) {
button.addEventListener("click", function () {
if (item == "dark") {
this.changeThemeTo(0);
} else if (item == "light") {
this.changeThemeTo(1);
}
});
}
}, this);
}
}
window.onload = function () {
theme.init();
}
Here my html code
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
.dark {
background-color: #191919;
color: #EEEEEE;
}
.light {
background-color: #EEEEEE;
color: #191919;
}
</style>
</head>
<body>
<div id="change-theme">
DARK
LIGHT
</div>
</body>
</html>
use self.changeThemeTo instead this.changeThemeTo. and define self = this as per the below code sample. also optimized some portion of the code.
var theme = {
changeThemeTo: function (theme_value) {
sessionStorage.setItem("THEME", theme_value);
document.body.className = theme_value;
},
init: function () {
document.body.classList.add("fade");
setTimeout(function () {
document.body.classList.remove("fade");
}, 100);
var themes = ['dark', 'light'];
var self = this;
themes.forEach(function (item) {
var button = document.querySelector("." + item);
if (button) {
button.addEventListener("click", function () {
self.changeThemeTo(item);
});
}
}, this);
}
}
window.onload = function () {
theme.init();
}
<style type="text/css">
.dark {
background-color: #191919;
color: #EEEEEE;
}
.light {
background-color: #EEEEEE;
color: #191919;
}
</style>
<div id="change-theme">
DARK
LIGHT
</div>
I am brand new at this so I apologize because I'm sure an intermediate could pull his or her answer from what's already been asked, but I need specific help.
I'm having trouble getting my "next" and "previous" buttons for my slideshow to work in Javascript. Once the user clicks through all 5 images, it needs to return to the first image, ready to click through again-- a continuous loop. I think arrays are supposed to be utilized. What am I missing?
Thank you!!
var imageCache = [];
var imageItem = 0;
var images = 0;
var captionNode;
var imageNode;
var $ = function (id) {
return document.getElementById(id);
}
window.onload = function () {
var listNode = $("image_list");
var nextButton = $("next");
var previousButton = $("previous");
captionNode = $("caption");
imageNode = $("image");
var links = listNode.getElementsByTagName("a");
var i, linkNode, image;
for ( i = 0; i < links.length; i++ ) {
linkNode = links[i];
// Pre-load image and copy title properties.
image = new Image();
image.src = linkNode.getAttribute("href");
image.title = linkNode.getAttribute("title");
imageCache.push(image);
}
// Now record the total images we have.
images = imageCache.length;
// Set up the button handlers.
nextButton.onclick = nextButtonClick;
previousButton.onclick = previousButtonClick;
}
function nextButtonClick() {
}
function previousButtonClick() {
}
article, aside, figure, figcaption, footer, header, nav, section {
display: block;
}
body {
font-family: Arial, Helvetica, sans-serif;
width: 380px;
margin: 0 auto;
padding: 20px;
border: 3px solid blue;
}
h1, h2, ul, p {
margin: 0;
padding: 0;
}
h1 {
padding-bottom: .25em;
color: blue;
}
h2 {
font-size: 120%;
padding: .5em 0;
}
ul {
display: none;
}
img {
height: 250px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Slide Show</title>
<link rel="stylesheet" href="main.css">
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<script src="slide_show.js"></script>
</head>
<body>
<section>
<h1>Fishing Slide Show</h1>
<ul id="image_list">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<h2 id="caption">Casting on the Upper Kings</h2>
<p>
<img src="images/casting1.jpg" alt="" id="image">
</p>
<input type="button" value="Previous" name="previous" id="previous">
<input type="button" value="Next" name="next" id="next">
</section>
</body>
</html>
You have the following variables:
var imageCache = [];
var imageItem = 0;
var images = 0;
Presumably imageItem is the index of the currently displayed image (e.g. 0 for the first one) and images is the number of images (i.e. imageCache.length). To get the next image:
imageItem = ++imageItem % images;
var nextImage = imageCache[imageItem];
This will wrap around to zero when imageItem reaches the number of images in the cache. Similarly for previous:
imageItem = (--imageItem + images) % images;
var prevImage = imageCache[imageItem];
so that when imageItem reaches 0, subtracting 1 goes to -1 and adding imageCache.length sets it to the last image. The rest of the time it's left at imageItem - 1.
It's up to you to fill in the rest of the code. :-)
I would use an array zipper to implement the next and prev functions. An array zipper is a data structure that allows you to move forward and backward through an array.
function ArrayZipper(array) {
var length = array.length, index = 0;
this.getCurrent = function () {
return array[index];
};
this.getNext = function () {
return array[index = (index + 1) % length];
};
this.getPrevious = function () {
return array[index = (length + index - 1) % length];
};
}
You can use an array zipper to create a slide show as follows:
var zipper = new ArrayZipper([ "black"
, "blue"
, "green"
, "cyan"
, "red"
, "magenta"
, "yellow"
, "white"
]);
var style = $("color").style;
style.backgroundColor = zipper.getCurrent();
$("next").addEventListener("click", function () {
style.backgroundColor = zipper.getNext();
});
$("prev").addEventListener("click", function () {
style.backgroundColor = zipper.getPrevious();
});
function $(id) {
return document.getElementById(id);
}
function ArrayZipper(array) {
var length = array.length, index = 0;
this.getCurrent = function () {
return array[index];
};
this.getNext = function () {
return array[index = (index + 1) % length];
};
this.getPrevious = function () {
return array[index = (length + index - 1) % length];
};
}
#color {
height: 100px;
width: 100px;
}
<div id="color"></div>
<button id="next">Next</button>
<button id="prev">Prev</button>
Hope that helps.
I'm trying to automatically adjust LTR styles to RTL with Javascript.
And for some reason, the Div elements are not affected
This is my HTML:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="rtl.js"></script>
<style type="text/css">
.r {float: right;}
.l {float: left;}
</style>
</head>
<body>
<div class="l">A</div>
<div class="r">B</div>
</body>
</html>
And this is the content of rtl.js
var allStyles=['azimuth','background','background-attachment','background-color','background-image','background-position','background-repeat','border','border-collapse',
'border-color','border-spacing','border-style','border-top','border-right','border-bottom','border-left','border-top-color','border-right-color','border-bottom-color',
'border-left-color','border-top-style','border-right-style','border-bottom-style','border-left-style','border-top-width','border-right-width','border-bottom-width',
'border-left-width','border-width','bottom','caption-side','clear','clip','color','content','counter-increment','counter-reset','cue','cue-after','cue-before',
'cursor','direction','display','elevation','empty-cells','float','font','font-family','font-size','font-size-adjust','font-stretch','font-style','font-variant',
'font-weight','height','left','letter-spacing','line-height','list-style','list-style-image','list-style-position','list-style-type','margin-top',
'margin-right','margin-bottom','margin-left','marker-offset','marks','max-height','max-width','min-height','min-width','orphans','outline','outline-color',
'outline-style','outline-width','overflow','padding-top','padding-right','padding-bottom','padding-left','page','page-break-after','page-break-before',
'page-break-inside','pause','pause-after','pause-before','pitch','pitch-range','play-during','position','quotes','richness','right','size','speak','speak-header',
'speak-numeral','speak-punctuation','speech-rate','stress','table-layout','text-align','text-decoration','text-indent','text-shadow','text-transform','top',
'unicode-bidi','vertical-align','visibility','voice-family','volume','white-space','widows','width','word-spacing','z-index','border-radius','border-top-left-radius',
'border-top-right-radius','border-bottom-left-radius','border-bottom-right-radius','border-image','border-image-outset','border-image-repeat','border-image-source',
'border-image-slice','border-image-width','break-after','break-before','break-inside','columns','column-count','column-fill','column-gap','column-rule',
'column-rule-color','column-rule-style','column-rule-width','column-span','column-width','#keframes','animation','animation-delay','animation-direction',
'animation-duration','animation-fill-mode','animation-iteration-count','animation-name','animation-play-state','animation-timing-function','backface-visibility',
'perspective','perspective-origin','transform','transform-origin','transform-style','transition','transition-delay','transition-duration','transition-timing-function',
'transition-property','background-clip','background-origin','background-size','overflow-x','overflow-y','overflow-style','marquee-direction','marquee-play-count',
'marquee-speed','marquee-style','box-shadow','box-decoration-break','opacity']
//'margin','padding'
function applyStyles($el,css_props,css_vals)
{
$el.removeAttr('style');
var i=0;
for (i=0;i<css_props.length;i++)
{
$el.css(css_props[i],css_vals[i]);
}
}
function str_swap(str,a,b)
{
return str.replace(b,"tovtov").replace(a,b).replace("tovtov",a);
}
function isCSSEmpty(val)
{
return (val == undefined)||(val == '')||(val == 'none')||(val == 'normal');
}
function transform_rtl()
{
var rtl='rtl_rtl_rtl';
jQuery('*').each(function() {
$el=jQuery(this);
if ($el.hasClass(rtl))
return;
var css_props = [];
var css_vals = [];
var i;
for (i=0;i<allStyles.length;i++)
{
var css_prop=allStyles[i];
var css_val=$el.css(css_prop);
if (!isCSSEmpty(css_val)){
if (css_prop.indexOf("left")>=0)
{
css_prop=css_prop.replace("left","right");
}
else if(css_prop.indexOf("right")>=0)
{
css_prop=css_prop.replace("right","left");
}
else
{
css_val=str_swap(css_val,"right","left");
css_val=str_swap(css_val,"rtl","ltr");
}
css_props.push(css_prop)
css_vals.push(css_val)
$el.css(css_prop,'');//clear styling
}
}
applyStyles($el,css_props,css_vals);
$el.css('diretion','rtl');
this.addClass(rtl);
});
}
jQuery(document).ready(transform_rtl);
replace jQuery('*').each(function() { with $(document).children('*').each(){ .I guess this should work!