Prośba o oconę kodu

0

Jskieś 5 miesięcy temu strałem się o przyjęcie do pracy i teraz treśc tego zadania brzmiała tak.

Stwórz stronę HTML, na której znajduje się 5 elementów <DIV>, każdy o rozmiarze 600x10px. Po najechaniu myszką na każdy z DIV rozwija się on płynnie do rozmiaru 600x200px. Po kliku pozostaje w rozmiarze pełnym, po zjechaniu z DIV bez kliknięcia, wraca on do małych wymiarów. Ponowne kliknięcie na zablokowany wcześniej DIV (600x200px), powinno przywracać jego oryginalne zachowanie, czyli automatyczne powracanie do wymiarów 600x10px po opuszczeniu jego obszaru przez kursor. Użyj jQuery/CSS.

Tutaj daje kod.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="pl" xml:lang="pl">
<head>
	<title>Test PHP - Zadanie 3</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link type="text/css" rel="stylesheet" href="../res/style.css"/>
	<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<div class="wrap">
<?php 
	$taskNum = 3;
	include __DIR__ . '/../res/nav.php';
	include __DIR__ . '/../res/tasks/'.$taskNum.'.php';
?>
</div>
<div class="solution">
<?php 
/** Obszar roboczy - miejsce na implementację rozwiązania zadania
 ************************************************************************
 * BEGIN >>
 ************************************************************************/
print ("




<div id=div1 style=\"width:600px; height:10px; background-color:blue;\"></div>
<br>
<div id=div2 style=\"width:600px; height:10px; background-color:blue;\"></div>
<br>
<div id= div3 style=\"width:600px; height:10px; background-color:blue;\"></div>
<br>
<div id= div4 style=\"width:600px; height:10px; background-color:blue;\"></div>
<br>
<div id= div5 style=\"width:600px; height:10px; background-color:blue;\"></div>



  <script language=\"javascript\">
var stan = true;
$(\"#div1\").mouseover(function(){
    if (stan == true) {
      $(\"#div1\").animate({height: '200px'});
    }
});  
$(\"#div1\").mouseout(function(){
  if (stan == true) {
  $(\"#div1\").animate({height: '10px'});
  }

});
  
$(\"#div1\").click(function(){
if (stan == true) {  
  $(\"#div1\").animate({height: '200px'});
  stan = false;
}
else if (stan == false) {
  $(\"#div1\").animate({height: '10px'});
  stan = true;
}
  

});



var stan2 = true;
$(\"#div2\").mouseover(function(){
    if (stan2 == true) {
      $(\"#div2\").animate({height: '200px'});
    }
});  
$(\"#div2\").mouseout(function(){
  if (stan2 == true) {
  $(\"#div2\").animate({height: '10px'});
  }

});
  
$(\"#div2\").click(function(){
if (stan2 == true) {  
  $(\"#div2\").animate({height: '200px'});
  stan2 = false;
}
else if (stan2 == false) {
  $(\"#div2\").animate({height: '10px'});
  stan2 = true;
}
  

});



var stan3 = true;
$(\"#div3\").mouseover(function(){
    if (stan3 == true) {
      $(\"#div3\").animate({height: '200px'});
    }
});  
$(\"#div3\").mouseout(function(){
  if (stan3 == true) {
  $(\"#div3\").animate({height: '10px'});
  }

});
  
$(\"#div3\").click(function(){
if (stan3 == true) {  
  $(\"#div3\").animate({height: '200px'});
  stan3 = false;
}
else if (stan3 == false) {
  $(\"#div3\").animate({height: '10px'});
  stan3 = true;
}
  

});


var stan4 = true;
$(\"#div4\").mouseover(function(){
    if (stan4 == true) {
      $(\"#div4\").animate({height: '200px'});
    }
});  
$(\"#div4\").mouseout(function(){
  if (stan4 == true) {
  $(\"#div4\").animate({height: '10px'});
  }

});
  
$(\"#div4\").click(function(){
if (stan4 == true) {  
  $(\"#div4\").animate({height: '200px'});
  stan4 = false;
}
else if (stan4 == false) {
  $(\"#div4\").animate({height: '10px'});
  stan4 = true;
}
  

});


var stan5 = true;
$(\"#div5\").mouseover(function(){
    if (stan5 == true) {
      $(\"#div5\").animate({height: '200px'});
    }
});  
$(\"#div5\").mouseout(function(){
  if (stan5 == true) {
  $(\"#div5\").animate({height: '10px'});
  }

});
  
$(\"#div5\").click(function(){
if (stan5 == true) {  
  $(\"#div5\").animate({height: '200px'});
  stan5 = false;
}
else if (stan5 == false) {
  $(\"#div5\").animate({height: '10px'});
  stan5 = true;
}
  

});
</script>
");





/************************************************************************
 * << END
 ************************************************************************/
?>
</div>
<?php include __DIR__ . '/../res/footer.php'; ?>
</body>
</html>
0

[OT]nie pracuje jako web ani front ani back a jedynie coś sobie czasem czytam/oglądam.[/OT]

Dla mnie ten kod to jeden wielki copy-paste.

  1. to style=\"width:600px; height:10px; background-color:blue;\" to powinien być styl i ten styl powinien mieć przypisany każdy element
  2. powinna być jedna funkcja w js rozwiń, jedna zwiń i jedna kliknij. Masz przecież diva na którym operacje się odbywają a nie PIĘĆ kopii tych funkcji
  3. nie jestem przekonany też do tych zmiennych stanx

moje rozwiązanie https://jsfiddle.net/2v15wzpm/2/

0

to style="width:600px; height:10px; background-color:blue;" to powinien być styl i ten styl powinien mieć przypisany każdy element
powinna być jedna funkcja w js rozwiń, jedna zwiń i jedna kliknij. Do każdej z nich możesz przekazać div na którym operacje się odbywają a nie PIĘĆ kopij tych funkcji

Tylko, że ja dostałem do edycji tylko kod php nie mogłem edytować styli css.

1

ech...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="pl" xml:lang="pl">
<head>
    <title>Test PHP - Zadanie 3</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link type="text/css" rel="stylesheet" href="../res/style.css"/>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<div class="wrap">
<?php 
    $taskNum = 3;
    include __DIR__ . '/../res/nav.php';
    include __DIR__ . '/../res/tasks/'.$taskNum.'.php';
?>
</div>
<div class="solution">
<?php 
/** Obszar roboczy - miejsce na implementację rozwiązania zadania
 ************************************************************************
 * BEGIN >>
 ************************************************************************/
print ("
<style type='text/css'>
div {
  width:600px; 
  height:10px; 
  background-color:blue;
}
</style>

<div data-value='0'></div>
<br>
<div data-value='0'></div>
<br>
<div data-value='0'></div>
<br>
<div data-value='0'></div>
<br>
<div data-value='0'></div>

<script  language='javascript'>
$('div').mouseover(function(){
  if ($(this).data('value') == '0')
		$(this).animate({height: '200px'});
});

$('div').mouseout(function(){
	if ($(this).data('value') == '0')
		$(this).animate({height: '10px'});
});

$('div').click(function(){
	var val = $(this).data('value') ^ 1;
	$(this).data('value', val);
});
</script>
");
 
/************************************************************************
 * << END
 ************************************************************************/
?>
</div>
<?php include __DIR__ . '/../res/footer.php'; ?>
</body>
</html>

EDIT: z tym, że tak jak pisałem - web to nie moja działka.
W moim kodzie nie podoba mi się data-value='0' i jestem pewny, że można to jakoś zgrabniej zrobić

0

A po co to bez tego też działa.

$('div').click(function(){
    var val = $(this).data('value') ^ 1;
    $(this).data('value', val);
});
0

żeby się nie zmniejszał jak go klikniesz

1

Ja to w ogóle nie wiem po co tam jQuery.
Na szybko zrobiłbym to jakoś tak:

<div class="container">
  <div class="container__element"></div>
  <div class="container__element"></div>
  <div class="container__element"></div>
  <div class="container__element"></div>
  <div class="container__element"></div>
</div>
.container {
  &__element {
    width: 600px;
    height: 10px;
    margin: 10px;
    background: skyblue;
    transition: height 1s;
    
    &:hover {
      height: 200px;
      transition: height 1s;
    }
    
    &--fixed {
      height: 200px;
    }
  }
}
document
  .querySelector('.container')
  .addEventListener('click', ({ target }) => {
    if (target.classList.contains('container__element')) {
      target.classList.toggle('container__element--fixed')
    }
  })

Demo: https://codepen.io/caderek/pen/dWdxZm

@abrakadaber To twoje rozwiązanie wariuje jak najedziesz kilkuktotnie na div.

0

A takie rozwiązanie będzie ok?

<!DOCTYPE html>
<html lang="pl">

<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <title>Magic rectangles</title>
   <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
   <![endif]-->
   <style>
      body {
         margin: 0;
         padding: 0;
      }
      
      div {
         box-sizing: border-box;
         border: 1px solid darkblue;
         margin: 10px;
         width: 600px;
         height: 20px;
         background-color: antiquewhite;
         -webkit-transition-property: height;
         -webkit-transition-duration: 1s;
         transition-property: height;
         transition-duration: 1s;
      }
      
      .big {
         height: 200px;
      }
      
      div:hover {
         height: 200px;
      }
   </style>
</head>

<body>
   <div></div>
   <div></div>
   <div></div>
   <div></div>
   <div></div>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
   <script>
      $(document).ready(function() {
         $("div").click(function() {
            $(this).toggleClass("big");
         });
      });
   </script>
</body>

</html>

1

@DevTomT

No lepiej niż oryginał, ale rozwiązania w moim kodzie są nieprzypadkowe:

  • nie przypinam listenerów do każdego elementu, tylko do kontenera - raz że listener jest jeden, dwa że dynamiczne dodanie kolejnego diva nie wymaga dodania dodatkowego listenera,
  • uzywam klas, dlaczego: https://4programmers.net/Forum/1360665,
  • od prefiksowania jest prefixer, nie klepie sie tego ręcznie

1 użytkowników online, w tym zalogowanych: 0, gości: 1