LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Javascript :: jQuery :: .animate() Causing Problems... (https://www.linuxquestions.org/questions/programming-9/javascript-jquery-animate-causing-problems-4175464958/)

cin_ 06-06-2013 06:26 AM

Javascript :: jQuery :: .animate() Causing Problems...
 
I am attempting to write some javascript that will create textareas and when you click on a textarea to begin typing it grows and centers in the window until you click off of it where it shrinks back down.

Easy enough, until I wanted to add the .animate() and suddenly I have some serious problems that I am pouring too much time into trying to figure out.

-If I drop focus on the textarea that is animating its growth while it is still animating then the .blur() function fails to call.

-If I shift focus to another textarea while the first is still animating
then both may remain large failing to call the .blur() function.

-Finally there is just some really strange activity with the centering feature. .scrollTo() and .animate() perform poorly together.


Any ideas on how to remedy any of these issues?


the javascript... boxy.js
Code:



function growthearea() {

$('textarea.textfield').blur(function(){
        $(this).animate({ height: "51" }, 500);  //shrink the current box when lose focus
  //$(this).height(51); 
});

        $('textarea.textfield').focus(function(){

    $("*").off("focus,blur,click");  //turn off focus,blur,click while animating
   
    var wheretoY = $(this).offset().top-73;
    window.scrollTo(17,wheretoY);
 
                // turn back on focus,blur,click after animation completes
                $(this).animate({ height: "409" }, 1000, function(){("*").on("focus,blur,click")});
                //$(this).height(409);
        });


}


function newboxbtn()
{
        var btn=document.createElement("textarea");
        btn.setAttribute('class','textfield');
       
        var textlocale = document.getElementById('locale');
        textlocale.appendChild(btn);

        $('textarea.textfield').on('keyup change', function() {
                $('p.display').text('You are typing: ' + $(this).val());  //live update from focused textarea
        });

        growthearea();  //recall function for any new boxes to be acknowledged

};

function jsinit()
{
        $('textarea.textfield').on('keyup change', function() {
                $('p.display').text('You are typing: ' + $(this).val());  //live update from focused textarea
        });
       
        growthearea(); //call function for initial group of boxes
}

the html... boxy.htm
Code:

<html>
<head>
        <link rel="stylesheet" type="text/css" href="sty.css" />               
        <script src="./jquery.js"></script>
        <script src="./boxy.js"></script>
<script>
        $().ready(function() {
                var $scrollingDiv = $("#scrollingDiv");
                $(window).scroll(function(){                       
                        $scrollingDiv
                                .stop()
                                .animate({"marginTop": ($(window).scrollTop() + 30) + "px"}, "fast" );                       
                });
                jsinit();
        });
</script>

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title></title>
</head>
<body>

        <div class="grid">
                                       
                <div class="col-left" id="left">
                                                <div class="module" id="scrollingDiv">
                        <input type="button" value="add" onclick="newboxbtn()" />
                        <p class="display">you are typing </p>
                        </div>
          </div> <!--div class="col-left"-->

 
                <div class="col-midd">
                        <div class="module" id="locale">
                                                        <textarea class="textfield" placeholder="begin typing here..." ></textarea>
                                                        <textarea class="textfield" placeholder="begin typing here..."></textarea>
            </div>
                  </div> <!--div class="col-midd"-->
 
        </div> <!--div class="grid"-->

</body>
</html>

the css... sty.css
Code:

.textfield {
        width: 97%;
        height: 51;
        resize: none;
        outline: none;
        border: none;
        font-family: "Lucida Console", Monaco, monospace;
        font-weight: 100;
        font-size: 70%;
        background: white;
/*        box-shadow: 1px 2px 7px 1px #0044FF;*/
}

.textfielded {
        width: 97%;
        resize: none;
        outline: none;
        border: none;
        overflow: auto;
        position: relative;
        font-family: "Lucida Console", Monaco, monospace;
        font-weight: 100;
        font-size: 70%;
        background: white;
/*        box-shadow: 1px 2px 7px #FFDD00;*/
}


/*#postcomp {
        width: 500px;
}*/


* {
  @include box-sizing(border-box);
}

$pad: 20px;

.grid {
  background: white;
  margin: 0 0 $pad 0;
 
  &:after {
    /* Or @extend clearfix */
    content: "";
    display: table;
    clear: both;
  }
}

[class*='col-'] {
        float: left;
  padding-right: $pad;
  .grid &:last-of-type {
          padding-right: 0;
  }
}
.col-left {
    width: 13%;
}
.col-midd {
        width: 43%;
}
.col-rght {
        width: 43%;
}

.module {
  padding: $pad;


}

/* Opt-in outside padding */
.grid-pad {
  padding: $pad 0 $pad $pad;
  [class*='col-']:last-of-type {
    padding-right: $pad;
  }
}

body {
        padding: 10px 50px 200px;
  background: #001235;

}
h1 {
  color: black;
        font-size: 11px;
        font-family: "Lucida Console", Monaco, monospace;
        font-weight: 100;
}
p {
        color: white;
        }



All times are GMT -5. The time now is 07:23 PM.