Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [JS] Wariujaca animacja
Maciek316
post 8.09.2015, 09:19:12
Post #1





Grupa: Zarejestrowani
Postów: 93
Pomógł: 0
Dołączył: 31.03.2012

Ostrzeżenie: (0%)
-----


Witam mam problem z animacja w moim skrypcie.
Wszystko jest okej dopóki nie zmienie karty w przeglądarce, wtedy gdy wróce to animacja robi co chce, przyspiesza, myli 2 efekty itp.


  1. $('.cios, #textDiv1').fadeIn()
  2. $('#textDiv1').animate({top: '-50px'},600);
  3. div1.innerHTML = -obrazenia[id];
  4. $('#textDiv1, .cios').fadeOut(1000)
  5. $('#textDiv1').animate({top: '0px'});


.cios i #textdiv1 odpowiadaja za wyswietlanie obrazka i obrazeń.

Okno odswiezam co 1,5 sekundy:

  1. var t=setTimeout('walcz()',1500);


Moge pokazać caly kod, jesli to pomoze :/
Go to the top of the page
+Quote Post
Comandeer
post 8.09.2015, 09:40:16
Post #2





Grupa: Zarejestrowani
Postów: 1 268
Pomógł: 254
Dołączył: 11.06.2009
Skąd: Świętochłowice

Ostrzeżenie: (0%)
-----


To jest problem związany z samym sposobem działania przeglądarki. Jeśli karta jest nieaktywna, to żadne zmiany w DOM nie są wprowadzane, bo nie ma takiej potrzeby. Stąd, gdy wrócisz na tę kartę, trzeba wprowadzić wiele zmian równocześnie. Prawdopodobnie przepisanie kodu na requestAnimationFrame by pomogło


--------------------
Go to the top of the page
+Quote Post
Maciek316
post 8.09.2015, 09:46:51
Post #3





Grupa: Zarejestrowani
Postów: 93
Pomógł: 0
Dołączył: 31.03.2012

Ostrzeżenie: (0%)
-----


No ale to jest dziwne, bo wyzej wymioniona animacja wariuje, a ta:
  1. $('#przeciwnik').animate({
  2. top: '+='+wartosci[id],
  3. width: '-='+wartosci[id],
  4. }, 700, function() {}
  5. );

juz tego problemu nie ma.
Go to the top of the page
+Quote Post
Comandeer
post 8.09.2015, 10:58:46
Post #4





Grupa: Zarejestrowani
Postów: 1 268
Pomógł: 254
Dołączył: 11.06.2009
Skąd: Świętochłowice

Ostrzeżenie: (0%)
-----


A ta animacja też jest odpalana cyklicznie?


--------------------
Go to the top of the page
+Quote Post
Maciek316
post 8.09.2015, 11:09:01
Post #5





Grupa: Zarejestrowani
Postów: 93
Pomógł: 0
Dołączył: 31.03.2012

Ostrzeżenie: (0%)
-----


tak, to jest w jednym if'ie
  1. if(kto[id] == 1){
  2. $('#przeciwnik').animate({
  3. top: '+='+wartosci[id],
  4. width: '-='+wartosci[id],
  5. }, 700, function() {}
  6. );
  7. $('.cios, #textDiv1').fadeIn()
  8. $('#textDiv1').animate({top: '-50px'},600);
  9. div1.innerHTML = -obrazenia[id];
  10. $('#textDiv1, .cios').fadeOut(1000)
  11. $('#textDiv1').animate({top: '0px'});
  12.  
  13. }
Go to the top of the page
+Quote Post
Comandeer
post 8.09.2015, 11:23:31
Post #6





Grupa: Zarejestrowani
Postów: 1 268
Pomógł: 254
Dołączył: 11.06.2009
Skąd: Świętochłowice

Ostrzeżenie: (0%)
-----


Hm… mój strzał byłby taki, że obliczenia zmian rozmiaru można zsumować i na koniec po prostu wprowadzić jedną zmianę. A pokazywanie i ukrywanie trzeba jednak wykonać tyle razy, ile zostało zaplanowane.

Można próbować w taki sposób coś z tym robić: http://stackoverflow.com/questions/1652986...-on-window-blur


--------------------
Go to the top of the page
+Quote Post
Maciek316
post 8.09.2015, 11:48:13
Post #7





Grupa: Zarejestrowani
Postów: 93
Pomógł: 0
Dołączył: 31.03.2012

Ostrzeżenie: (0%)
-----


no ale jak to sprawdzić, jaki dać warunek? ohmy.gif
Problem jest w animacji obrazen, bo cala reszta sie zgadza.

Ten post edytował Maciek316 8.09.2015, 12:11:15
Go to the top of the page
+Quote Post
Comandeer
post 8.09.2015, 18:07:27
Post #8





Grupa: Zarejestrowani
Postów: 1 268
Pomógł: 254
Dołączył: 11.06.2009
Skąd: Świętochłowice

Ostrzeżenie: (0%)
-----


Jak strona jest nieaktywna, to nie wywołuj setTimeout i usuń obecną kolejkę animacji przy pomocy clearTimeout. Jak wykryć, że strona jest nieaktywna: http://stackoverflow.com/questions/1060008...rrently-active/


--------------------
Go to the top of the page
+Quote Post
Maciek316
post 9.09.2015, 19:48:56
Post #9





Grupa: Zarejestrowani
Postów: 93
Pomógł: 0
Dołączył: 31.03.2012

Ostrzeżenie: (0%)
-----


tam jest pokazany caly kod, ale jak ja mam z tego stworzyc warunek, pod ktora zmienna jest to stworzone?

Pomoże ktoś? :/
Go to the top of the page
+Quote Post
Comandeer
post 9.09.2015, 20:32:17
Post #10





Grupa: Zarejestrowani
Postów: 1 268
Pomógł: 254
Dołączył: 11.06.2009
Skąd: Świętochłowice

Ostrzeżenie: (0%)
-----


Ale z czym dokładnie masz problem?


--------------------
Go to the top of the page
+Quote Post
Maciek316
post 10.09.2015, 08:36:14
Post #11





Grupa: Zarejestrowani
Postów: 93
Pomógł: 0
Dołączył: 31.03.2012

Ostrzeżenie: (0%)
-----


ze stworzeniem warunku, ogolnie js sie dopiero ucze sad.gif

Dajmy na to pierwszą sytuacje:
  1. (function($) {
  2. if (!$.winFocus) {
  3. $.extend({
  4. winFocus: function() {
  5. var init = true, methods = [];
  6. if (!$(document).data('winFocus')) $(document).data('winFocus', $.winFocus.init());
  7. for (x in arguments) {
  8. if (typeof arguments[x] == 'object') {
  9. if (arguments[x]['blur']) $.winFocus.methods.blur.push(arguments[x].blur);
  10. if (arguments[x]['focus']) $.winFocus.methods.focus.push(arguments[x].focus);
  11. if (arguments[x]['blurFocus']) $.winFocus.methods.blurFocus.push(arguments[x].blurFocus);
  12. if (arguments[x]['initRun']) init = arguments[x].initRun;
  13. }
  14. else if (typeof arguments[x] == 'function') methods.push(arguments[x]);
  15. else if (typeof arguments[x] == 'boolean') {
  16. init = arguments[x];
  17. }
  18. }
  19. if (methods) {
  20. if (methods.length == 1) $.winFocus.methods.blurFocus.push(methods[0]);
  21. else {
  22. $.winFocus.methods.blur.push(methods[0]);
  23. $.winFocus.methods.focus.push(methods[1]);
  24. }
  25. }
  26.  
  27. if (init) $.winFocus.methods.onChange();
  28. }
  29. });
  30. $.winFocus.init = function() {
  31. // var document.hidden || document.msHidden || document.webkitHidden || document.mozHidden;
  32.  
  33. // Standards:
  34. if ($.winFocus.props.hidden in document) // IE10 | FF20+
  35. document.addEventListener('visibilitychange', $.winFocus.methods.onChange);
  36. else if (($.winFocus.props.hidden = 'mozHidden') in document) // Older FF Versions (?)
  37. document.addEventListener('mozvisibilitychange', $.winFocus.methods.onChange);
  38. else if (($.winFocus.props.hidden = 'webkitHidden') in document) // Chrome
  39. document.addEventListener('webkitvisibilitychange', $.winFocus.methods.onChange);
  40. else if (($.winFocus.props.hidden = 'msHidden') in document) // IE 4-6
  41. document.addEventListener('msvisibilitychange', $.winFocus.methods.onChange);
  42. else if (($.winFocus.props.hidden = 'onfocusin') in document) // IE7-9
  43. document.onfocusin = document.onfocusout = $.winFocus.methods.onChange;
  44. else // All others:
  45. window.onpageshow = window.onpagehide = window.onfocus = window.onblur = $.winFocus.methods.onChange;
  46.  
  47. return $.winFocus;
  48. }
  49. $.winFocus.methods = {
  50. blurFocus: [], blur: [], focus: [],
  51. exeCB: function(e) {
  52. if ($.winFocus.methods.blurFocus) $.each($.winFocus.methods.blurFocus, function(k, v) { this.apply($.winFocus, [e, !e.hidden]) });
  53. else if (e.hidden) if ($.winFocus.methods.blur) $.each($.winFocus.methods.blur, function(k, v) { this.apply($.winFocus, [e]) });
  54. else if ($.winFocus.methods.focus) $.each($.winFocus.methods.focus, function(k, v) { this.apply($.winFocus, [e]) });
  55. },
  56. onChange: function(e) {
  57. var eMap = { focus: false, focusin: false, pageshow: false, blur: true, focusout: true, pagehide: true };
  58. e = e || window.event;
  59.  
  60. if (e) {
  61. e.hidden = e.type in eMap ? eMap[e.type] : document[$.winFocus.props.hidden];
  62. $(window).data('visible', !e.hidden);
  63. $.winFocus.methods.exeCB(e);
  64. }
  65. else {
  66. try { $.winFocus.methods.onChange.call(document, new Event('visibilitychange')); }
  67. catch(err) { }
  68. }
  69. }
  70. }
  71. $.winFocus.props = { hidden: 'hidden' }
  72. }
  73. })(jQuery);


i daje warunek:
  1. $.winFocus(function(event, isVisible) {
  2. if(isVisible){
  3. var t=setTimeout('walcz()',1500);
  4. }else
  5. clearTimeout(t);
  6.  
  7. });

I wtedy po zmienieniu karty tak jakby sie zamraza, ale potem animacja wariuje, pokazuje po 2 naraza, przyspiesza :/

Ten post edytował Maciek316 10.09.2015, 09:03:06
Go to the top of the page
+Quote Post
Comandeer
post 10.09.2015, 13:33:17
Post #12





Grupa: Zarejestrowani
Postów: 1 268
Pomógł: 254
Dołączył: 11.06.2009
Skąd: Świętochłowice

Ostrzeżenie: (0%)
-----


Raczej coś tego typu:
Kod
var t;
$.winFocus(function(event, isVisible) {
if(isVisible){
    t=setTimeout('walcz()',1500);
}else
    clearTimeout(t);

});

W innym wypadku clearTimeout nie ma czego usuwać, bo nie widzi t


--------------------
Go to the top of the page
+Quote Post
Maciek316
post 10.09.2015, 13:36:42
Post #13





Grupa: Zarejestrowani
Postów: 93
Pomógł: 0
Dołączył: 31.03.2012

Ostrzeżenie: (0%)
-----


teraz, gdy zmieniam karte to zatrzymuje, ale animacja mi sie posypala, pokazuje obie naraz i przyspieszają :/
Go to the top of the page
+Quote Post
Comandeer
post 10.09.2015, 13:45:25
Post #14





Grupa: Zarejestrowani
Postów: 1 268
Pomógł: 254
Dołączył: 11.06.2009
Skąd: Świętochłowice

Ostrzeżenie: (0%)
-----


http://jsfiddle.net/Comandeer/u4kp1ycd/


--------------------
Go to the top of the page
+Quote Post
Maciek316
post 10.09.2015, 14:22:27
Post #15





Grupa: Zarejestrowani
Postów: 93
Pomógł: 0
Dołączył: 31.03.2012

Ostrzeżenie: (0%)
-----


przerobilem to na swoj kod, ale u mnie dziala na odwrot ohmy.gif jak zmienie podstrone to sa animacje, ale jak na niej jestem to sie zamraza... juz nie mam do tego sily tongue.gif
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 14.08.2025 - 11:35