Java_객체 및 이벤트

MINJOO·2022년 6월 14일
0

객체

01.object

<!-- 리터럴표기법으로 객체 만들기 -->
<!DOCTYPE html>
<html lang="ko">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>사용자 정의 객체</title>
	<link rel="stylesheet" href="css/obj.css">
</head>
<body>
	<div id="display"></div>
	<div id="display2"></div>

<script>
	var toyRobot = {
		productId : "123-12",
		name : "Robot",
		price :25000,
		quantity: 10,
		showStock: function(){
			document.querySelector('#display').innerHTML = this.name + " 제품은 " + this.quantity + "개 남았습니다.";
		},
		showPrice: function(){
			// 제품의 가격은 XXX 입니다.
			document.querySelector('#display2').innerHTML = this.name + " 제품은 " + this.price + "입니다.";
		}
	};
	toyRobot.showStock();
	toyRobot.showPrice();

</script>
</body>
</html>

css 코드

		#display {
			width:500px;
			margin:20px auto;
			padding:10px;
			border:1px solid #ccc;
			text-align:center;
			font-size:20px;
			line-height:30px;
		}

결과물

D-day계산

<!doctype html>
<html lang="ko">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>D-Day for love</title>
	<link href="css/d-day.css" rel="stylesheet">
</head>
<body>
	<div class="container">
		<div class="day1">
			<h3>우리 만난지</h3>
			<p id="accent" class="accent"><span style="font-size:0.6em; font-style:italic">며칠?</span></p>
			</div>
			<div class="bar">기념일 계산</div>
			<div class="day2">
				<ul>
					<li class="item-title">100일</li>         
					<li class="item-date" id="date100"></li>
				</ul>
				<ul>
					<li class="item-title">200일</li>
					<li class="item-date" id="date200"></li>
				</ul>     
				<ul>
						<li class="item-title">1년</li>
						<li class="item-date" id="date365"></li>
					</ul>      
				<ul>
							<li class="item-title">500일</li>
							<li class="item-date" id="date500"></li>
						</ul>      									     
			</div>
		</div>

		<script src="js/dday-result.js"></script>
	</body>
	</html>

JS코드

var now = new Date(); 
var firstDay = new Date("2019-05-05");
var toNow = now.getTime(); 
var toFirst = firstDay.getTime();
var passedTime = toNow - toFirst ; //오늘 - 처음 만난날 
var passedDay = Math.round( passedTime / (24*60*60*1000));
document.querySelector('#accent').innerText = passedDay + "일";

// 100일, 200일, ... 입력되는 날수에 따른 계산 
function calcDate(days){
	var future = toFirst + days *(24*60*60*1000);
	var someday = new Date(future);
	var year = someday.getFullYear();
	var month = someday.getMonth() + 1;
	var date = someday.getDate();

	document.querySelector('#date'+days).innerText = year+ "년" + month + "월" +  date + "일";
}

calcDate(100);
calcDate(200);
calcDate(365);
calcDate(500);

css 코드

@import url('https://fonts.googleapis.com/css?family=Black+Han+Sans|Jua');

* {
	box-sizing: border-box;
}
.container{
  width:450px;
  margin:0 auto;
  /* background:url(images/heart.png) no-repeat 5px -100px;
  background-size:500px;   */
  border:1px solid #ccc;
  border-radius:2%;
  box-shadow:2px 2px 5px #333;
}
.day1{
  padding-top:20px;
  text-align:center;  
}
.day1 h3 {
  font-size:1.2em;
  color:#666;
}
.accent{
  margin-left:10px;
	margin-right:10px;
	margin-top:10px;
	font-family: 'Jua', sans-serif;
	font-weight:bold;
  font-size:3.5em;
  color:#222;
}
.bar {
	width:100%;
	margin:60px auto 0 auto;
	padding-left:15px;
	height:40px;	
	background:#747474;
	color:#fff;
	font-size:1.2em;
	line-height:40px;
}
.day2 {
  width:420px;
  margin:20px auto 20px auto;
}
.day2 ul {
  list-style: none;
  border-bottom:1px dashed #ccc;
  height:60px;
}
.day2 ul:last-child {
	border-bottom:none;
}
.item-title {
  float:left;
  width:160px;        
  font-weight:bold;
  font-size:1.5em;
  line-height:60px;
}
.item-date {
  float:left;
  margin-left:60px;
  font-size:1.2em;
  color:#222;
  text-align:right;
  line-height:60px;
}

결과물

이벤트

민들레씨 소개

<!DOCTYPE html>
<html lang="ko">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>자바스크립트 이벤트</title>
	<link rel="stylesheet" href="css/event.css">	
</head>
<body>
	<div id="item">
		<img src="images/flower1.jpg" alt="">
		<button class="over" id="open" onclick="showDetail()">상세 설명 보기</button>
		<div id="desc" class="detail">
			<h4>민들레</h4>
			<p>어디서나 매우 흔하게 보이는 잡초로서 바닥에 딱 붙어서 꽃봉오리 하나가 쏙 올라온다. 톱니 모양의 잎새와 눈에 확 띄는 노란 꽃이 인상적이다. 특히 꽃이 지고나면 솜털모양의 깃을 가진 씨앗들이 나오는데 바람을 타고 날아가서 널리 퍼진다.</p>
			<button id="close" onclick="hideDetail()">상세 설명 닫기</button>
		</div>
	</div>	
	
	<script src="js/event.js"></script>
</body>
</html>  

css코드

		#item {
			position:relative;
			width:500px;
			height:auto;
			padding:15px 20px;
			margin:auto;
		}
		button {
			background-color:rgba(255,255,255,0.7);;
			padding:5px;
			border:1px solid #ccc;
			font-size:0.8em;			
		}
		.over {
			position:absolute;
			left:30px;
			bottom:30px;
		}
		.detail {
			width:400px;
			text-align:left;			
			line-height:1.8;
			display:none;
		}
		#cover { border: 5px solid transparent;}

js코드

		function showDetail() {  // '상세 설명 보기'를 클릭했을 때 상세 설명을 보여주는 함수
			document.querySelector('#desc').style.display = "block";	// 상세 설명 부분을 화면에 표시
			document.querySelector('#open').style.display = "none";   // '상세 설명 보기' 단추를 화면에서 감춤
		}
		
		function hideDetail() {  // '상세 설명 닫기'를 클릭했을 때 상세 설명을 감추는 함수
			document.querySelector('#desc').style.display = "none";	   // 상세 설명 부분을 화면에서 감춤
			document.querySelector('#open').style.display = "block";	 // '상세 설명 보기' 단추를 화면에 표시
		}

결과물

느낀점

html과 js&CSS코드를 어떻게 연결하는지 보니 신기하다. 홈페이지가 어떻게 만들어지고 작동되는지 이젠 완벽히 이해할수 있을것같다.

profile
코딩 신생아

0개의 댓글