Как анимировать размер ширины с левой стороны с помощью JS?

Использование xmlstarlet:

echo '<job xmlns="http://www.sample.com/">programming</job>' | \
   xmlstarlet sel -N var="http://www.sample.com/" -t -m "//var:job" -v '.'
2
задан Scrimothy 1 March 2019 в 14:49
поделиться

2 ответа

Вот CSS-версия анимации.

Я использую визуально скрытый флажок, кнопка - label, которая управляет флажком, и анимации CSS переключаются в состоянии проверки.

Единственное, что он выполняет начальную анимацию в обратном порядке при загрузке.

Редактировать: Я на самом деле исправил это с небольшим изменением CSS (:not(indeterminate)) и дополнительным фрагментом JS при загрузке, который устанавливает флажок indeterminate.

const checkbox = document.getElementById('sign-in-state');
checkbox.indeterminate = true;
@keyframes login {
	0% {
		left: 0;
		width: 40%;
	}
	50% {
		width: 60%;
	}
	100% {
		left: 60%;
		width: 40%;
	}
}
@keyframes logout {
	0% {
		left: 60%;
		width: 40%;
	}
	50% {
		width: 60%;
	}
	100% {
		left: 0%;
		width: 40%;
	}
}

.sign-in-checkbox:not(:indeterminate):not(:checked) + .box .square {
  --animation-name: login;
}

.sign-in-checkbox:not(:indeterminate):checked + .box .square {
	--animation-name: logout forwards;
}

.window {
  position: absolute;
  width: 100%;
  height: 100%;
  background: #c73030;
  z-index: 9999;
}
.box {
  position: absolute;
  top: 50%;
  left: 50%;
		overflow: hidden;
  transform: translate(-50%, -50%);
  width: 100vw;
  height: 100vh;
  background-color: #f7986c;
  z-index: -999;
}

.square {
		animation: var(--animation-name) 600ms reverse ease-in-out;
  position: absolute;
		right: auto;
  top: 0;
  width: 40%;
  height: 100%;
  background-color: cornflowerblue;
}


.btn {
	display: flex;
	align-items: center;
	justify-content: center;
	color: white;
	font-family: sans-serif;
  position: absolute;
  width: 200px;
  height: 70px;
  top: 80%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #444;
}

.visually-hidden { /* https://snook.ca/archives/html_and_css/hiding-content-for-accessibility */
    position: absolute !important;
    height: 1px; width: 1px;
    overflow: hidden;
    clip: rect(1px 1px 1px 1px); /* IE6, IE7 */
    clip: rect(1px, 1px, 1px, 1px);
}
<input type="checkbox" id="sign-in-state" class="visually-hidden sign-in-checkbox">		
<div class="box">

			<div class="square">
				<label class="btn" for="sign-in-state">
					
					<div class="sI">Sign In</div>
				</label>
			</div>

		</div>

0
ответ дан Scrimothy 1 March 2019 в 14:49
поделиться

Проверьте это:

<!DOCTYPE html>
<html>

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <style>
        .window {
            position: absolute;
            width: 100%;
            height: 100%;
            background: #c73030;
            z-index: 9999;
        }

        #box {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 1000px;
            height: 500px;
            background-color: #f7986c;
            z-index: -999;
        }

        #square {
            position: absolute;
            top: 0;
            width: 400px;
            height: 100%;
            background-color: cornflowerblue;
        }

        #btn {
            position: absolute;
            width: 300px;
            height: 70px;
            top: 80%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: #444;
        }
    </style>

</head>

<body>
    <div id="box">

        <div id="square">
            <button id="btn">
                <div id="sI">Sign In</div>
            </button>
        </div>

    </div>


    <script>
        var animated = false;

        function animate() {
            $('#square').animate({
                width: '600',
                right: '0'
            }, 300, 'swing');
            $('#square').animate({
                width: '400'
            }, 300, 'swing');
            $('#square').css("left", "");
            animated = true;
        }

        function reverseAnimate() {
            $('#square').animate({
                width: '600',
                left: '0'
            }, 300, 'swing');
            $('#square').animate({
                width: '400'
            }, 300, 'swing');
            $('#square').css("right", "");
            animated = false;
        }

        $('#btn').click(function() {
            if (!animated) { //If it has not been animated, animate!
                animate();
            } else {
                reverseAnimate();
            }
        });
    </script>
</body>

</html>

Я использовал jQuery и изменил несколько вещей, чтобы они выглядели немного как пример. Кроме того, мне понравилось ваше использование .reverse (), но я не использовал его в моем случае.

0
ответ дан Bedir 1 March 2019 в 14:49
поделиться
Другие вопросы по тегам:

Похожие вопросы: