Sunday, March 29, 2015

Positioning with CSS

Hi again,
#menu1{          /*this is a grandpa*/
    background-color: white;
    position: relative;/*this is for the logo balls to refer to this bar in positioning*/
    height: 15em;
}
#menu_pos{    /*this is a parent*/
    position: absolute;
    left: 50%;   
    top: 50%;/*at a certain position through relative to grandpa*/
    height: 3em;
...
}
.menu{            /*this is a child*/
    display: inline-block;
    position: relative;
    top: 5%; /*this is relative to the parent, not grandpa! Even though grandpa is relative!*/
    /*another note: this top positioning depends on the parent having a 'height'! I.e. if "height: 3em;" was commented in the parent then "top: 5%;" here will be ignored*/
    width: 7em;   
    height: 3em;
}

Sunday, February 8, 2015

JQuery

This is how the file script.js is  referred to in the html file:
<script type="text/javascript" src="script.js"></script>
I note that </script> is needed since script is not a self-closing tag.

$(document).ready(...)       ready means when the document is loaded, so what's inside the parentheses will be executed when it loads. This "ready" is like an "if" I guess but a function of the "document" JQuery object.
 Inside the "ready" function we can put a set of instructions (like a function definition) like this
function{ ... }

$( "div").fadeOut(1000);   means the div will disappear gradually, it will take 1000 msec to do so.

'div' also works instead of "div".

$('#id_name').fadeOut(1000);

$('div').slideDown('slow');

$('div').mouseenter(function(){ /*this is also like an if statement*/
     $('div').hide();  
    }) /*when the mouse is over any div block, all the divs will hide.*/

$('div').mouseenter(function(){
     $('div').fadeTo('slow',0.25);  
    }); /*0.25 is the opacity value; 1 is maximum*/

$('div').mouseleave(function(){
     $('div').fadeTo('fast',1);  
    });

$(this).hover(function{...},function{...}); /*the first function is for when the mouse enters the object and the second is when the mouse is out of it.*/
I tested it and compared it to using the mouseenter(...) and mouseleave(...) both, and the result was the same.

$('div').click(function(){ ... };

$('div').fadeOut('slow');
$('div').fadeIn('slow');  

var $div = $('div');
or maybe
var divis = $('div');

A class also has a JQuery object:
var $divTypeOne = $('.class_name');
or even an id:
var $divOne = $('#id_name');
or a compound selector
var $liAndP = $('li, p');

$(this) refers to the event-handler (which is e.g. the JQuery object that the mouse is over or on which the mouse is clicked)

One variable definition can be:
$p = $("<p>I'm a new paragraph!</p>");
$(".info").append("<p>I'm the new last child added to the class info!</p>");
$(".info").prepend("<p>I'm the new first child added to the class info!</p>");
$("<p>I'm the new first child added to the class info!</p>").prependTo(".info");
$("<p>I'm the new last child added to the class info!</p>").appendTo(".info"); 
$(".info").after("<p>I'm a child added after the class info!</p>");
This creates (adds) a new paragraph.
 
but
 $(".info1").after($("p"));
moves the existing $("p") paragraph after class .info1
 
A side note: 
for some reason this isn't working:
$(document).ready(function () {
    $(".container:nth-child(2)").after("<p>sdfas</p>");
}); 
but this works: 
$(document).ready(function () {
    $(".container.first-child").after("<p>sdfas</p>");
}); 
It was in lesson 3/14 of Modifying HTML Elements. 


$("p").remove(); /*removes the element (and all its descendants, normally, I guess)*/

$("p").empty(); /*keeps the element, but empties it and removes its descendants*/

We want to add a class to an element; let's see an example
 $('#text').addClass('class_name');
Notice that class_name is not preceded by '.' because the name is sufficient and we're not selecting an element.

 $('#text').removeClass('class_name');

$('#text').toggleClass('highlighted'); /*adds the class if removed and removes the class if added*/

Any particular attribute can be changed, e.g. 
$('div').height('200px'); /*we could've also written*/
$('div').css('height','200px');

$('div').css('border-radius','10px');

To set the value of an element (it's the first found element)
$(document).ready(function(){
       $("p").html("jQuery magic in action!");
})

To get the value:
alert($("p").val());  /*it didn't show anything for some reason?!*/
It's working like this though:
var toAdd = $('input[name=checkListItem]').val();

This event handler on() can work on elements or selectors that might exist sometime after the webpage has loaded.
$(document).on('event', 'selector', function() {
    Do something!
});
e.g.
$(document).on('click', '.item', function(){
    $('this').remove();

});

Another event handler is the dblclick()
$('div').dblclick(function(){ ... }

$('input').focus(function(){...}) /*when the HTML input selector gets focus, either by using the tab or by clicking into it, this event handle is triggered*/

$(document).keydown(function(){
        $('div').animate({left: '+=10px'},500); })

This one is ok too.
  $(document).on('keydown',$(document),function(){
        $('div').animate({left:'+=10px'},500);
    });

$(document).keydown(function(pressed_key){ /*this is an input to the function triggered by the event handler*/
/*   37 is the left key, then top, then right, then down.*/
/*   pressed_key.which is the value of the pressed key.*/
        $('div').animate({left: '+=10px'},500); })
We might use parseInt(pressed_key.which, 10) means to consider the number originally as decimal.
parseInt(pressed_key.which, 16) means to consider the number originally as hexadecimal and tranform it into decimal.

$('img').animate({top: "+=10px"}, 'fast'); /*this lets the image go down*/
$('img').animate({top: "-=10px"}, 'fast');  /*this lets the image go up*/

This one doesn't work unfortunately
$('img').animate({bottom: "+=10px"}, 'fast');
nor this one
$('img').animate({right: "+=10px"}, 'fast');

To have the explode effect of a div e.g., we have to include an extra script tag that is the JQueryUI library:
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
and that's the syntax:
$(this).effect('explode');

Another effect is the bounce(... , ...) :
$('div').effect('bounce', {times:3},500);

$('div').effect('slide');

$("#menu").accordion(...); /*where #menu is the id of a particular div structured as a menu*/
Even .accordion() with no argument is ok.

$('#car').draggable(); /*we can click on it and drag it*/

$('div').resizable(); /*it resized just from the left!?*/

$('ol').selectable(); /*maybe this works best with lists, this adds the clicked li into the class ui-selected*/
an alternative can be this one found here
$("li").bind('click', function(){  /*or $('li').click(function(){ */
   // remove the ui-selected class if it's there
   if($("li.ui-selected").length) $("li.ui-selected").removeClass('ui-selected');
   // add the ui-selected class to the clicked element
   $(this).addClass('ui-selected');
}); 
And in the stylesheet.css text file we can add whatever we like in this selector:
ol .ui-selected { ... }

$('ol').sortable(); /*to rearrange the lists as we like*/








Friday, February 6, 2015

Working with CSS

1) Before CSS, the styling was inline with the tag, e.g.
     <p style="font-size: 4px; ...">text goes here</p>
2)  but there is another way of styling by including the <style> tag in <head> here </head>, e.g.
<head>
      <style>
              p{
                     font-size: 4px;
                     ...
                   }
      </style>
</head>
3) The third way being the separate .css file
     p{
           font-size: 4px;
           ...
        }
As for the span, it's for exceptions within tags
     span{
           font-size: 6px;
           ...              }
Anyway, this is the generic form:
selector1, selector2, ...{
          property1:...;  /*a selector is actually a tag like <p> but without the < and >*/
          property2:...;
          ...
             }

Some selectors and their properties:
body{
 background-image: url('http://bit.ly/UpQgJ6');
}

a{                                /*this is for the text links*/
  color: #cc000;
  text-decoration: none;
}

img{
    height: 100px;
    width: 300px;
    border: 1px solid #4682b4;

    display: block;/*this makes it in a new line as opposed to inline-block;*/                           
    margin: auto;/*this makes the image go to the center from left and right sides*/
    /*margin: 10px 20px 30px 40px; this is the order of margins: top right bottom left*/

    /*or we can do*/
    /* margin-top: ...*/
    /* margin-rigth: ...*/
    /* margin-bottom: ...*/
    /* margin-left: ...*/
    /*The margin is part of the box or block and is different from the concept of the position!*/
    /*It keeps the box away from other boxes in case of inline-block e.g.*/
}/*this is written of course in the css file, but in the html file if we want to apply this special style to a particular image we would do like the following*/
/*<img src="..." height="100px" width="300px" border="1px solid #4682b4"/> */
/*notice the = instead of the : inside the img tag*/

/*here the div makes a button shape*/
/*A div can have a border like a table or a cell. A div can be centered as well.*/
div{
     margin: auto; /*this is to center the div block; its content can be placed to the right maybe within the div*/
     /*margin-left: 5px;*/
     background-color: #BCD2EE;/*the height, width, and background-color should be specified */
     height: 50px;                            /*to let the div appear*/
     width: 120px; /*this is simply the width of the div block*/
     /*the % could be used to the width, e.g. width: 45%;*/
     /*or even to left: 45%;*/

     border-color: #6495ED;
     border-style: dashed;
     border-width:3px;
     /*border: 3px dashed #6495ED;   using the previous 3 may be better than this one alone*/
     border-radius: 15px; /*this round the edges of the div*/
     /*border-radius: ...%; like 50% is a circle*/
     /*border-bottom-left-radius: 15px;*/
     text-align: center; /*just like in a p tag, this is for the text only*/


     /*divs have by default display: block; notice:*/
     /in the case of  a <p> inside a <div>, the width of the paragraph made by the p tag is that of the block and doesn't take the width of the page*/
     /*if 2 successive divs have display: inline-block; then they are on the same line, but the element is still a block box meaning it is affected by the allocated by the allocated height and width*/
/*but the text or paragraph inside the div e.g. will control the positioning of the divs, so we need to add     vertical-align: middle;*/
     /* display: inline; on the same line but  not a block*/
     /*display: none;    don't display it (even its children _if any_ will disappear).*/

     margin-left: 5px; /*a margin might have a negative value*/
}
When we want to obtain a horizontal menu, we can use an unordered list and display it as inline.


/*amazingly this selector * selects everything!*/
*{
    border: 1px solid black; /*every element is bordered*/
}
so everything has a border, including p, img, ... as stated here.
In fact, any html element is made of a content, surrounded by a padding, surrounded by a border, surrounded by a margin.
So padding is the distance between the border and the actual content e.g. the paragraph text.
padding: ...px; is a padding on all sides

In positioning we make use of:
float: ...; /*The value can be right e.g.*/
Floating elements will not overlap one on top of the other, but a floating element may overlap a non-floating element.
I noticed that in order for the element to be floating it has to have a specified width and height or else it won't work because it already covers the whole page width (in case of a div e.g.).
Suppose only the height is specified, we could use
clear: left;  /*this puts the block for example below any floating block on the left*/
clear: right;/*or*/
clear: both;

More on positioning:
Positions of elements is static by default.
Position: absolute; relative; or fixed; /*fixed means to the screen*/
When position: static; the Left: 50px; e.g. has no effect. I think static positioning is either below or on the same line and can flow (float: right e.g.) if we want to.
When position: absolute; then Left: 50px; would work. It is positioned relative to the first non-static-positioned parent, I think the parent might be absolute-, relative-, or fixed-positioned. In case that parent didn't exist then it is positioned relative to the <html> page.
When position: fixed; then n Left: 50px; would work. It is positioned relative to the browser window. It is fixed even while scrolling the window.
When position: relative; it is positioned relative to the container of the div e.g..

It's worthy to note that float may sometimes contradict with the positioning, so float wins as it seems.

/*to only affect e.g. the p tags that are inside e.g. the div tag we write:*/
div p{
        ...
}
/*but simply a p will affect all the p tags everywhere*/
p{
   ...
}

so div p{ ...} affects all the ps that are descendants of the higher div; but if we want to affect only the ps that are directly children of a top-level div (I mean div directly nested in the body) then we use:
div > p {
            ...
}

A programmer defined selector can be:
#id_name{
              ...
}
this id name is typical to be in a particular tag in the html file, e.g.:
<h2 id="id_name">...</h2>

To apply a styling to multiple different tags, we use the programmer defined class, e.g.
.class_name{
                  ...
}
this class name is probably in many tags in the html file, e.g.
<h1 class="class_name">...</h1>
<p class="class_name">...</p>

By the way, one tag can have an id and belong to a certain class at the same time, e.g.
<p class="class_name" id="id_name">...</p>

 The id has a higher priority than a class in case of contradiction.

And now comes the turn of the pseudo-class; the general form is:
selector:pseudo-class_selector {
    property: value;
}
It seems like the pseudo-class_selector is a predefined value, as well as the selector, the following examples are all about links:
a:hover {
    color: #cc0000;
    font-weight: bold;
    text-decoration: none;
}
a:visited{ /*a link that was clicked on*/
          ...
}
a:link{ /*a link that was not visited*/
          ...
}

Another example of the pseudo-class selector is:
p:first-child{ /*the first <p> child in any parent will be colored in red; the parent can be div, li, or ...*/
    color: red;
}
this works just like a class would but easier.
In this example instead of the p tag being the selector we could had have any other tag even a programmer defined class is ok.
But consider the following html snippet:
  <div>
       <p>I'm the first child!</p>
       <p class="class-name">I'm the second child</p>
       <p class="class-name">I'm the third child</p>
       <p>I'm the fourth child</p>
  </div>
and this css snippet:
.class-name:first-child{
                               ...
}
well the first child is considered the absolute one which corresponds to the paragraph "I'm the first child!" and not to "I'm the second child", so the html snippet won't be affected by the css pseudo-class snippet.
As if saying: "if the class-name affected tag was the first child do ..."

Likewise for the second child, it would be for example
p:nth-child(2){
                  ...
}
As if saying: "if the p tag was the second child do ...", thus <h3> e.g. can be the first child and <p> is the second one so it will be affected, but say <h2> was the second child then it won't be affected.

And the 5th child
p:nth-child(5){
                  ...
}

In my programming terms I would say that the pseudo selector related to the link a are somehow working at runtime, but the first-child and nth-child(...) are working at code-time.

The magnificent z-index property, the higher the value the more it will be showing on the webpage.

opacity: 0.5; this interesting feature is to how much brightness maybe an element would have.



To hide the remaining of an image within the parent container div element, we might use:
overflow: hidden;
















Monday, February 2, 2015

learn HTML from scratch

Thank you codecademy!
Almost from scratch, this glossary can be a good reference to HTML.
I just learned it from this awesome website, I followed the pace as if I were a 10 years old guy.

Some detailed notes about texts:
To use the colors that can be used in CSS, like in the "style" attribute of the <p> or <h2> tag, we write something like this: <p style="color: violet"> text goes here ...
Well, the list of colors can be found here.

As for the font type of a text, which is used for example in a listed item like this:
<li style="font-family: Arial">text goes here...
The list of fonts is here.

I noticed that in the value of the font-size attribute, when used for example like this in the h3 tag:
<h3 style="font-size: 14px">text goes here...
the px (pixels) has to be connected to the assigned number (14 in the example).

It's inspiring how the background-color in the style attribute affects the whole body, e.g.
<body style="background-color: brown">text may go here
it's a pity that this same background-color maybe has no effect when used in the <title> tag.

     About text sizes:
I noticed in my nearly 15 inch Toshiba laptop that
<p style="font-size: 1em">
is the same as
<p style="font-size: 16px">

     About text fonts:
1) Three font-family property values are built-in by default (serif, sans-serif, and cursive), and the rest can be imported (I should not rely on the installed fonts in a user's computer).
2) In case we suspect that the font might not exist on the user's computer, we can put another one next to the first, so if it didn't find the first it takes the next, e.g.
<p style="font-family: Verdana, cursive, serif">

The style="font-weight: bold;" is also interesting and probably an alternative to <strong>;
and style="text-decoration: underline" is an alternative to <em>.

      About texts:
&lt; is the <
and &gt; is the >

About images:
<img> is a self closing tag meaning it is
 <img src="https://www.google.com.lb/images/srpr/logo11w.png"/>
and not  <img src="https://www.google.com.lb/images/srpr/logo11w.png"> ... </img>

About tables:
A cell can merge into another one(s) to the right, e.g. this one spans till  1 cell further
<td colspan="2">....

The border attribute sets the border to the whole table
<table border="1px">
 while
<table style="border-collapse:collapse;"> <!-- this removes all borders of the table-->
usually allows any cell to specify the padding of the row
<th style="padding: 2px">
and the thickness of side border
<td style="border-left:1px solid black;"> <!--or-->
<td style="border-right:1px solid black;"> <!--or-->
<td style="border-top:1px solid black;"> <!--or-->
<td style="border-bottom:1px solid black;"> <!--or-->
<td style="border:1px solid black;">

<div> is useful to create hyper-linked menu items, and to affect a division (section) with CSS.
(More on the attributes of the div can be found in the "Working with CSS" post in this blog.)

<div> works with CSS on a section level, while <span> works on small scale (like a work or letter).

another tag:
<button>Click me!</button>
probably the button is not a block by default.

another tag:
<br/> this goes down a line.

About forms:
<form name="Form1">
       <input type="text" name="EntryItem" value="default value" />
</form>

outline-color: red;   this is a style that can be put in the input tag, it turns the color of the edge of the input box into red.
outline-style: solid;   this one is needed in Firefox in order for the outline-color to work.







Sunday, February 1, 2015

Learn JavaScript from scratch

Well, this JavaScript glossary helps you learn the language basics.

My remarks:
JavaScript looks a type-loose easy language. The basics are not much different than C.
Anyway these are my notices:
1) The functions have 2 ways to be written. The non-hoisted form -which looks strange compared to what I know in C- is little ugly.
 I didn't see as well a function prototype as in C, but JavaScript took care of this by using the concept of a hoisted function. A function can be hoisted right from its definition; it has a pretty look (as in C), and the non-hoisted function has the ugly look.
2) I wasn't use to the console in C, but in the mentioned reference they covered the console which is useful for debugging since we can use the "Inspect Element (Q)" in Firefox by right-clicking in the browser.
3) As in object oriented programming (OOP), we can make an object without even defining a class. I haven't seen such thing in C++! This is really flexible. We're targeting 2 birds with one stone: a) declaring an object - b) without previous class or struct definition.
More on this is here.
4) Also in OOP, the mentioned reference webpage stated only the inheritance.

Slash and backslash

Ever noticed that windows explorer uses the backslash while the web browser uses the slash!