Rotating Box With CSS and JavaScript - javascript

Okay so I have a box that I made in CSS and what I want it to do is basically spin. However the part that I am lost at is, how do I control the speed at which box spins at using a range/slider?
This is my HTML Code:
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="box.css" />
<link rel="javascript" href="spin.js" />
</head>
<body>
<div id="box">
<p id="text">Something goes here...</p>
</div>
<div id="control">
<input id="controlo" type="range" min="10" max="50" step="5" value="25" />
</div>
</body>
</html>
This is my CSS Code:
body{
text-align:center;
}
#box{
display:block;
width:150px;
margin:150px auto;
padding:15px;
text-align:center;
border:7px solid blue;
background:red;
-webkit-transition: -webkit-transform 1.5s linear;
}
.eg {
-webkit-transform: rotate(360deg);
}
And finally the JavaScript Code:
var cur = 0;
function doit() {
var speed = +$("#controlo").val();
cur = (cur + speed);
$("#box").css("-webkit-transform", "rotate(" + cur + "deg)");
}
setInterval(doit, 100);
I cant get this box to spin. I don't know whats wrong but please help!

You're including the JavaScript wrong.
You should include it like this:
<script type="text/javascript" src="spin.js"></script>
and not like this:
<link rel="javascript" href="spin.js" />
You should also make sure that you've included jQuery before. So prepend
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

I pasted your code straight into jsfiddle.net at this link http://jsfiddle.net/86D7Z/ and it worked just fine. I can't seem to find your problem.
Edit: I can't believe I missed what Micha spotted. Time for bed.

Related

JQuery timepicker plugin hidden behind modal window

I was searching for answer on the internet and on stack's other questions, but none of the solutions are working for me. I'm trying this for days. My timepicker JQuery plugin appears behind the triggered modal window. I don't have much time left before the science fair and I'm really in trouble. I would be really, really greatful if someone knows how to help me. Cheers guys and thanks in advance. Here's the code and picture:
Form section
<div class='form-group'>
<label for='start' class='col-xs-4 control-label'>Starting time</label>
<div class='col-xs-5'>
<?php include('test.php'); ?>
</div>
</div>
Test.php
<html>
<head>
<style type="text/css" media="all">#import "timePicker.css";</style>
<style type="text/css">
input {
margin:0;
padding:0;
}
pre {
background:#fff;
border:1px solid #ddd;
padding:4px;
}
.ui-timePicker {
position: relative;
z-index: 10000 !important;
}
</style>
<meta charset='utf-8'>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
<script type="text/javascript" src="jquery.timePicker.js"></script>
</head>
<body>
<p><input id="time1" type="text" class="time form-control" /></p>
<script>
$("#time1").timePicker();
</script>
</body>
</html>
Have you tried just changing the z index of your time picker?
<style>
#time1{
z-index:1151 !important;
}
</style>

Getting javascript function to work

I'm working on a website for a school project and I'm having trouble with using the javascript. I am not a fan of js because I always have issues with it and I can never seem to get them figured out without help, but it is one of the requirements for the project. I am trying to make the title in the page move in from off the left side of the page and into the center when the page loads. Here is my code:
JS:
function animate() {
function movetext() {
obj = document.getElementById('spacer');
obj.style.position ='relative';
obj.style.left = '-100px'; //still needs to be adjusted
}
function moveright() {
obj.style.alignContent="center";
obj.style.animation="move 2s";
}
}
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Lockport, NY - Activities</title>
<link rel="stylesheet" href="project.css">
<link href='https://fonts.googleapis.com/css?family=Oleo+Script+Swash+Caps' rel='stylesheet' type='text/css'>
</head>
<body onload="animate();">
<!-- Background -->
<div class="background">
<!-- Navigation -->
<?php
include 'includes/nav.php';
?>
<!-- Title -->
<div id="spacer"><p span class="titles">Activities...</p></div>
<div id="wrapper">
<div id="main">
//code removed for readability
</div>
<!-- Footer -->
<?php
include 'includes/footer.php';
?>
</div>
</div>
</body>
<script src="project1.js"></script>
</html>
I know I'm not the greatest when it comes to coding, and I know its probably a simple fix but I can't find it. Help is appreciated.
You can do this easily with CSS animations:
setTimeout(function() {
document.getElementById('spacer').classList.add('show');
}, 100);
#spacer {
position: absolute;
right: 100%;
transition: right 3s;
}
#spacer.show {
right: 50%;
}
<div id="spacer">
<p class="titles">
Hello
</p>
</div>
A more elegant solution for this would be to use two classes (e.g. .centerpos, .title) and just assign the class with the styling for centering the text via js.
So it would look something like that:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Lockport, NY - Activities</title>
<link href='https://fonts.googleapis.com/css?family=Oleo+Script+Swash+Caps' rel='stylesheet' type='text/css'>
</head>
<style>
.title {
position: absolute;
left: -100px;
transition: left 2s;
}
.title.centerpos {
left: 50%;
transform: translate(-50%, 0);
}
</style>
<body onload="document.querySelector('.title').classList.add('centerpos');">
<div class="background">
<div id="spacer"><p span class="titles title">Activities...</p></div>
<div id="wrapper">
<div id="main">
//code removed for readability
</div>
</div>
</div>
</body>
</html>

Changing Colour of a Div with JS

So, I have a simple onclick function set up that is supposed to change the background of a div. Here is my JS code;
var colour = document.getElementById("backgroundColour");
function changeToColour1() {
colour.style.backgroundColor = "#47535F";
}
function changeToColour2() {
colour.style.backgroundColor = "#82A3B2";
}
Here is my HTML code;
<button id="colour1" class="colours" onclick="changeToColour1()"></button>
<button id="colour2" class="colours" onclick="changeToColour2()"></button>
<div id="backgroundColour"></div>
How would I get the div to change its colour when the button is clicked?
a much cleaner way of doing this would be to just use javascript to add classes to your elements, and let CSS control the colors.
$('#colour1').on('click',function(){
$('#backgroundColour').removeClass('colour1 colour2').addClass('colour1');
});
$('#colour2').on('click',function(){
$('#backgroundColour').removeClass('colour1 colour2').addClass('colour2');
});
and in your CSS put
#backgroundColour.colour1 {
background-color: #47535F;
}
#backgroundColour.colour2 {
background-color: #82A3B2;
}
Give the Div a proper width and height or some content, otherwise the
div cannot be seen.
Avoid inline event handlers and use DOM3 event handlers.
You can also use data-* attributes to store the colors instead of
having a seperate function for each button.
HTML
<button id="colour1" class="colours" data-color="#47535F" >Click me</button>
<button id="colour2" class="colours" data-color="#82A3B2">Click me</button>
<div id="backgroundColour">fff</div>
JS
var colourDiv = document.getElementById("backgroundColour");
var colours = document.getElementsByClassName('colours');
for(var i=0; i< colours.length; i++) {
colours[i].addEventListener('click', function() {
var color = this.getAttribute('data-color');
colourDiv.style.backgroundColor = color;
});
}
Check Fiddle
first and foremost i recommend you to use JQuery for the task you are trying to accomplish, im recommending you do for the simple fact JQuery is a simple straight forward library for javascript that makes our lives as coders a lot more simple, now that im done saying that here is the code.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" /><meta http-equiv="content-language" content="en-US" />
<title>...</title>
<style type="text/css">
html, body{
width:auto;
height:auto;
}
#changing{ /*this is the css for our javascript*/
margin:0px;
padding:0px;
width:100%;
height:100%;
position:absolute;
top:0px;
left:0px;
right:0px;
bottom:0px;
}
#wrap{ /*this is to keep the browse menu that has your buttons from disappearing below the changing div*/
margin:0px;
padding:0px;
width:100%;
height:40px;;
background-color:#000000;
border-bottom:5px solid #D8D8D8;
border-radius:0px;
position:fixed;
top:0px;
left:0px;
z-index:10;
}
#button1, #button2{
margin:0px;
padding:0px;
width:auto;
height:auto;
position:fixed;
}
#button1{
top:0px;
left:0px;
}
#button2{
top:0px;
right:0px;
}
</style>
<script rel="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>
<div id="body-container">
<div id="wrap">
<input type="button" id="button1" value="click me" />
<input type="button" id="button2" value="click me 2" />
</div>
<div id="changing">
</div>
</div>
<script>
$("#button1").click(function(){
$("#changing").css("background-color", "#47535F");//changes the color of the background to #47535F
})
$("#button2").click(function(){
$("#changing").css("background-color", "#82A3B2");//changes the color of the background to #82A3B2
})
</script>
</body>
</html>
in this code we use inline css and javascript with the JQuery library, we first start the document how we should always start a html document by adding the DOCTYPE and our meta tags with the attributes content-type and content-language we also add a title.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" /><meta http-equiv="content-language" content="en-US" />
<title>...</title>
</head>
<body>
</body>
</html>
next we add our our script to import the JQuery library, i usually go with the google hosted version because its usually up and running and i dont have to constantly update it , google does that for you, the only thing you need to do is add the new url in the src to replace the old version of JQuery.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" /><meta http-equiv="content-language" content="en-US" />
<title>...</title>
<script rel="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>
</body>
</html>
next we add our html, in our html we added two buttons and placed them in a div called wrap and added our changing div and placed both the changing div and the wrap div into a div called body-container.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" /><meta http-equiv="content-language" content="en-US" />
<title>...</title>
<script rel="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>
<div id="body-container">
<div id="wrap">
<input type="button" id="button1" value="click me" />
<input type="button" id="button2" value="click me 2" />
</div>
<div id="changing">
</div>
</div>
</body>
</html>
now time to add the icing to the cake with our script and css, here we added our css to determine the style of our page we made ours so that the browse menu were are buttons are located scroll with the page and are always onto, now onto the script, we made our script so that if the button is pressed it will change the background of the div body-container.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" /><meta http-equiv="content-language" content="en-US" />
<title>...</title>
<style type="text/css">
html, body{
width:auto;
height:auto;
}
#changing{ /*this is the css for our javascript*/
margin:0px;
padding:0px;
width:100%;
height:100%;
position:absolute;
top:0px;
left:0px;
right:0px;
bottom:0px;
}
#wrap{ /*this is to keep the browse menu that has your buttons from disappearing below the changing div*/
margin:0px;
padding:0px;
width:100%;
height:40px;;
background-color:#000000;
border-bottom:5px solid #D8D8D8;
border-radius:0px;
position:fixed;
top:0px;
left:0px;
z-index:10;
}
#button1, #button2{
margin:0px;
padding:0px;
width:auto;
height:auto;
position:fixed;
}
#button1{
top:0px;
left:0px;
}
#button2{
top:0px;
right:0px;
}
</style>
<script rel="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>
<div id="body-container">
<div id="wrap">
<input type="button" id="button1" value="click me" />
<input type="button" id="button2" value="click me 2" />
</div>
<div id="changing">
</div>
</div>
<script>
$("#button1").click(function(){
$("#changing").css("background-color", "#47535F");//changes the color of the background to #47535F
})
$("#button2").click(function(){
$("#changing").css("background-color", "#82A3B2");//changes the color of the background to #82A3B2
})
</script>
</body>
</html>
i hoped this helped, for any information on JQuery i recommend you go visit their online documents, they have a lot of useful info there, here is the link
http://api.jquery.com/
you can also check out w3chools they have a lot of good stuff on JQuery as well as many other programming languages, here is the link
http://www.w3schools.com/
best of luck with your project or learning the javascript language (=

flippy jquery effect doesn't rotate correctly

I have a problem with flip effect on jquery plugin.When the image rotates it does not rotate correctly.I dont know why but i spent hours and no solution please help.
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="Flip-jQuery/jquery-1.9.1.js"></script>
<script src="Flip-jQuery/jquery-ui-1.7.2.custom.min.js"></script>
<script src="Flip-jQuery/jquery.flip.min.js"></script>
<script src="Flip-jQuery/jquery.flip.js"></script>
<title>Untitled Document</title>
<script type="text/javascript">
$(document).ready(function(){
$("#box1").on('mouseover',function(){
$(".flip").flip({
direction:'lr',
content:'hello',
color:'red'
});
})
$('.revert').on('click',function(){
$('.flip').revertFlip();
})
});
</script>
#main{
border:1px solid red;
width:500px;
height:auto;
position:absolute;
}
#box1, #box2{
border:2px solid black;
width:100px;
height:100px;
margin:10px;
cursor:pointer;
}
</style>
<div id='main'>
<div id='box1' class='flip'>
box1
</div>
<div id='box2'>
box2
</div>
<a style=" float:right;" href="#" class="revert">Click to revert</a>
</div>
Here is the whole code.I have tried anything but nothing.
Thanks in advance.
Try this
$(document).ready(function(){
$("#box1").on('mouseover',function(){
$(".side2").flip({
direction:'lr', //in the flip API direction seems to come first in every example
content:'hello',
color:'red'
});
})
});
You also have a syntax error in your HTML:
<div id='main'>
<div id='box1' class='side1'"> <!-- HERE remove that double quote -->
box1
</div>
<div id='box2' class='side2'>
box2
</div>
</div>
You have included the flip.js plugin twice in your HTML header.
Check this thing i get what the problem was. Basically you are triggering twice the mouse over event JavaScript/jQuery: event fired twice

jQuery Progress bar and counter

I searched alot about this and tried some of my own but I can't seem to make it work.
What I'm trying to realize is a progress bar that adds 10 or subtracts 10 every time you press a button.
Example when you press the button UP you add 10 to the progress bar and when you press the button DOWN you remove 10 from the bar.
Whatelse I'd want to realize is a script that can "read" the status of the progressbar and with an if or else function display a text from a paragraph (If > 50 it'll fadeIn a text, else < 50 it'll fadeIn a different text)
Hope I explained myself well and hope anyone can help me. I'm still new in jQuery/JavaScript.
jQuery UI has a built-in progress bar that you can do what you want easily.
http://jqueryui.com/demos/progressbar/
Here's a simple implementation of what you're trying to do:
http://jsfiddle.net/makotosan/bW5Wd/3/
Here is a little example to get you started; there are a lot of capabilities to this script, don't be afraid to experiment a little bit:
<!doctype html>
<html lang="en" dir="ltr">
<head>
<title>Progress Bar</title>
<meta charset="utf-8" />
<style>
#progress {
position:relative;
width:25px;
height:100px;
border:2px solid #000;
background-color:#ccc;
}
#progress div {
position:absolute;
bottom:0;
height:0;
width:25px;
background-color:#f00;
}
span {
margin:10px auto;
}
</style>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(function(){
// Define your variables
var interval=10;
var half=50;
var max=100;
var texts=['Less then 50...','More then 50!'];
var upButton=$('button[name="up"]');
var downButton=$('button[name="down"]');
var bar=$('#progress').find('div');
upButton.on('click',function(){
var height=bar.height();
if(height>=0 && height<max) {
var newHeight=parseFloat(height+interval,10);
bar.css('height',newHeight);
$('span').hide().text((newHeight<half) ? texts[0] : texts[1]).fadeIn();
}
});
downButton.on('click',function(){
var height=bar.height();
if(height>0 && height<=max) {
var newHeight=parseFloat(height-interval,10);
bar.css('height',newHeight);
$('span').hide().text((newHeight<half) ? texts[0] : texts[1]).fadeIn();
}
});
});
</script>
</head>
<body>
<h1>Progress Bar</h1>
<p>
<button name="up">Add</button> <button name="down">Remove</button>
</p>
<div id="progress">
<div> </div>
</div>
<span></span>
</body>
</html>

Categories