gpt4 book ai didi

javascript - 如何自动滚动到列表中最后插入的值(非底部)。[[注 :List is Sorted]]

转载 作者:行者123 更新时间:2023-11-30 14:10:35 25 4
gpt4 key购买 nike

我正在尝试显示数组中的动漫列表。我已经使用“oredrby”过滤器对列表进行了排序。每次添加新行时,我都想自动滚动到列表中最后插入的行。请注意,我不想自动滚动到列表底部。这是

DEMO

演示我的问题。

<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<title>Angular App</title>
<style>
body{
font-family: Verdana;
font-weight: bold;
display: flex;
justify-content: center;
overflow: auto;
}
div{
width: 100%;
}
li{
position: relative;
animation: drop 0.7s;
cursor: pointer;
}
@keyframes drop{
0%{
height: 0;
box-shadow: 0 -5px 10px;
}
}
ul{
text-align: center;
margin: 0;
padding: 0;
margin-bottom: 0px;
font-size: 40px;
background-color: orange;
list-style-type: none;
scroll-behavior:
/* height: 75%; */

}
li:nth-child(odd){
display: flex;
justify-content: center;
align-items: center;
font-size: 40px;
height: 50px;
background-color: white;
transition: all 0.3s;
}
li:nth-child(even){
display: flex;
justify-content: center;
align-items: center;
font-size: 40px;
height: 50px;
background-color: orange;
transition: all 0.3s;
}
li:nth-child(odd) span{
position: absolute;
display: block;
right: 0px;
width: 50px;
height: inherit;
background-color: #ff3333;
}
li:nth-child(even) span{
position: absolute;
display: block;
right: 0px;
width: 50px;
height: inherit;
background-color: white;
}
li:hover{
box-shadow: 0px -10px 30px;
height: 80px;
font-size: 60px;
}
form{
/* height: 100px; */
bottom:0;
position: sticky;
/* width:100%; */
background-color: #081327;
color: white;
font-size: 40px;
justify-content: center;
align./-items:center;
}
.error{
display: flex;
opacity: 1;
justify-content: center;
align-items: center;
font-size: 40px;
height: 50px;
background-color: black;
color: #ff3333;
z-index: 2;
animation: show 0.7s;
}
.error span{

position: absolute;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
right:0px;
width: 50px;
height: inherit;
background-color: lightblue;
color: #ff3333;
transition:all 0.5s;
}
.error span:hover{
background-color: red;
color: black;
}
li span{
position: absolute;
transition: background-color 0.3s;
right:0px;
cursor: pointer;
}
li span:hover{
background-color: red;
}

form input{
border: 0;
height: 100px;
width:500px;
font-weight: bold;
font-size:40px;
text-align: center;
font-family: Verdana;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
box-shadow: 10px -10px 30px;
}
form button{
border: 0;
height: 100px;
width:300px;
font-size:40px;
margin-bottom:20px;
font-family: Verdana;
font-weight: bold;
background-color: #ff5413;
border-top-right-radius: 10px;
border-bottom-right-radius: 10px;
box-shadow: 10px -10px 30px;
}

@keyframes show{
/* 20%{
background-color: #1c1c1c;
}
40%{
background-color: black;
}
60%{
background-color: #1c1c1c;
}
80%{
background-color: black;
}
100%{
background-color: #1c1c1c;
} */
0%{

transform-origin: center;
height: 0px;
width: 0px;
font-size: 0px;
}
100%{
height: 50px;
font-size: 40px;
transform-origin: center;
transform: rotateX(360deg);
}
}
#text{
display: flex;
position: absolute;
justify-content: center;
align-items: center;
height: 100%;
position: fixed;
}
#textinfo{
display: flex;
position: relative;
border-radius: 15px;
font-size:40px;
border: solid black 5px;
justify-content: center;
align-items: center;
height: 200px;
text-align: center;
min-width:400px;
overflow: hidden;
width: auto;
background-color: white;
animation:hello 0.5s;

}


@keyframes hello{
0%{
height:0px;
font-size:0px;
}
100%{
height:200px;
font-size:40px;
}

}
#textinfo span{
position: absolute;
top: 10px;
right: 10px;
animation: pop 1.4s;
cursor: pointer;
/* animation-delay: 0.9s; */
}
@keyframes pop{
0%{

transform:translateY(-1000px);
}
50%{
transform:translateY(0px);
transform: rotateY(0deg);
}
100%{
transform: rotateY(1turn);
}
}

/* #description{
height:200px;
animation:flow 5s;
} */
/* @keyframes flow{
0%{
height:0px;
}
100%{
height:200px;
}
} */

</style>

<body ng-app="AngularApp" ng-controller="AngularAppCtrl">
<div>
<ul>
Anime List
<!-- <li ng-repeat="x in anime" ng-click="info(x.anime,x.hero)">
{{x.anime}}<span ng-click="removeAnime($index)">&times;</span></li> -->
<li ng-repeat="x in anime | orderBy" ng-click="info(x)">{{x}}
<span ng-click="removeAnime(getIndex(x))">&times;</span></li>
</ul>
<form ng-submit="addAnime()" id="c">
<div class="error" ng-show="existError">Anime Exists<span ng- click="removeErr()">&times;</span></div>
<center>Enter New Anime<br>

<input type="text" ng-model="newanime" ng- change="checkAnime()" placeholder="Enter Here!" style="color: black;"><button
type="submit">Add Anime</button>

</center>
</form>
</div>
<div id="text" ng-show="infoyes">
<!-- <div id="textinfo">{{name}}<br>{{inform}}</br><span style="color:#ff3333" ng-click="removeInfo()">&times;</span></div> -->
<div id="textinfo">{{name}}</br><span style="color:#ff3333" ng-click="removeInfo()">&times;</span></div>
</div>
</body>
<script>
var app = angular.module('AngularApp', []);
app.controller('AngularAppCtrl', function ($scope, $location, $http, $anchorScroll) {
//For fetching data from json
/* $http.get('anime.json').then(function(response){
$scope.anime = response.data.animes;
});*/

$scope.anime = ['Dragon Ball Z', 'One Punch Man', 'Naruto', 'One Piece', 'Yu Yu Hakusho', 'Tokyo Ghoul', 'Pokemon', 'Cowboy Bebop'];
$scope.addAnime = function () {
if (!$scope.newanime) { return; }
if ($scope.anime.indexOf($scope.newanime) == -1) {
/*var split = $scope.newanime.split(':');
$scope.anime.push({'anime': split[0],'hero':split[1]});*/
$scope.anime.push($scope.newanime);
$scope.newanime = "";
$scope.existError = false;
$location.hash('c');
$anchorScroll();
}
else {
$scope.existError = true;
}
};
$scope.removeErr = function () {
$scope.existError = false;
};
$scope.removeAnime = function (x) {
$scope.anime.splice(x, 1);
};
$scope.checkAnime = function () {
if ($scope.anime.indexOf($scope.newanime) == -1) {
$scope.existError = false;
}
else {
$scope.existError = true;
}
};
/*$scope.info = function (x,details) {
$scope.infoyes = true;
$scope.name = x;
$scope.inform = details;

};*/
$scope.info = function (x) {
$scope.infoyes = true;
$scope.name = x;

};
$scope.removeInfo = function () {
$scope.infoyes = false;
};
$scope.getIndex = function (x) {
return $scope.anime.indexOf(x);
}
});
</script>

</html>



<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<title>Angular App</title>
<style>
body{
font-family: Verdana;
font-weight: bold;
display: flex;
justify-content: center;
overflow: auto;
}
div{
width: 100%;
}
li{
position: relative;
animation: drop 0.7s;
cursor: pointer;
}
@keyframes drop{
0%{
height: 0;
box-shadow: 0 -5px 10px;
}
}
ul{
text-align: center;
margin: 0;
padding: 0;
margin-bottom: 0px;
font-size: 40px;
background-color: orange;
list-style-type: none;
scroll-behavior:
/* height: 75%; */

}
li:nth-child(odd){
display: flex;
justify-content: center;
align-items: center;
font-size: 40px;
height: 50px;
background-color: white;
transition: all 0.3s;
}
li:nth-child(even){
display: flex;
justify-content: center;
align-items: center;
font-size: 40px;
height: 50px;
background-color: orange;
transition: all 0.3s;
}
li:nth-child(odd) span{
position: absolute;
display: block;
right: 0px;
width: 50px;
height: inherit;
background-color: #ff3333;
}
li:nth-child(even) span{
position: absolute;
display: block;
right: 0px;
width: 50px;
height: inherit;
background-color: white;
}
li:hover{
box-shadow: 0px -10px 30px;
height: 80px;
font-size: 60px;
}
form{
/* height: 100px; */
bottom:0;
position: sticky;
/* width:100%; */
background-color: #081327;
color: white;
font-size: 40px;
justify-content: center;
align./-items:center;
}
.error{
display: flex;
opacity: 1;
justify-content: center;
align-items: center;
font-size: 40px;
height: 50px;
background-color: black;
color: #ff3333;
z-index: 2;
animation: show 0.7s;
}
.error span{

position: absolute;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
right:0px;
width: 50px;
height: inherit;
background-color: lightblue;
color: #ff3333;
transition:all 0.5s;
}
.error span:hover{
background-color: red;
color: black;
}
li span{
position: absolute;
transition: background-color 0.3s;
right:0px;
cursor: pointer;
}
li span:hover{
background-color: red;
}

form input{
border: 0;
height: 100px;
width:500px;
font-weight: bold;
font-size:40px;
text-align: center;
font-family: Verdana;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
box-shadow: 10px -10px 30px;
}
form button{
border: 0;
height: 100px;
width:300px;
font-size:40px;
margin-bottom:20px;
font-family: Verdana;
font-weight: bold;
background-color: #ff5413;
border-top-right-radius: 10px;
border-bottom-right-radius: 10px;
box-shadow: 10px -10px 30px;
}

@keyframes show{
/* 20%{
background-color: #1c1c1c;
}
40%{
background-color: black;
}
60%{
background-color: #1c1c1c;
}
80%{
background-color: black;
}
100%{
background-color: #1c1c1c;
} */
0%{

transform-origin: center;
height: 0px;
width: 0px;
font-size: 0px;
}
100%{
height: 50px;
font-size: 40px;
transform-origin: center;
transform: rotateX(360deg);
}
}
#text{
display: flex;
position: absolute;
justify-content: center;
align-items: center;
height: 100%;
position: fixed;
}
#textinfo{
display: flex;
position: relative;
border-radius: 15px;
font-size:40px;
border: solid black 5px;
justify-content: center;
align-items: center;
height: 200px;
text-align: center;
min-width:400px;
overflow: hidden;
width: auto;
background-color: white;
animation:hello 0.5s;

}


@keyframes hello{
0%{
height:0px;
font-size:0px;
}
100%{
height:200px;
font-size:40px;
}

}
#textinfo span{
position: absolute;
top: 10px;
right: 10px;
animation: pop 1.4s;
cursor: pointer;
/* animation-delay: 0.9s; */
}
@keyframes pop{
0%{

transform:translateY(-1000px);
}
50%{
transform:translateY(0px);
transform: rotateY(0deg);
}
100%{
transform: rotateY(1turn);
}
}

/* #description{
height:200px;
animation:flow 5s;
} */
/* @keyframes flow{
0%{
height:0px;
}
100%{
height:200px;
}
} */

</style>

<body ng-app="AngularApp" ng-controller="AngularAppCtrl">
<div>
<ul>
Anime List
<!-- <li ng-repeat="x in anime" ng-click="info(x.anime,x.hero)">
{{x.anime}}<span ng-click="removeAnime($index)">&times;</span></li> -->
<li ng-repeat="x in anime | orderBy" ng-click="info(x)">{{x}}
<span ng-click="removeAnime(getIndex(x))">&times;</span></li>
</ul>
<form ng-submit="addAnime()" id="c">
<div class="error" ng-show="existError">Anime Exists<span ng- click="removeErr()">&times;</span></div>
<center>Enter New Anime<br>

<input type="text" ng-model="newanime" ng- change="checkAnime()" placeholder="Enter Here!" style="color: black;"><button
type="submit">Add Anime</button>

</center>
</form>
</div>
<div id="text" ng-show="infoyes">
<!-- <div id="textinfo">{{name}}<br>{{inform}}</br><span style="color:#ff3333" ng-click="removeInfo()">&times;</span></div> -->
<div id="textinfo">{{name}}</br><span style="color:#ff3333" ng-click="removeInfo()">&times;</span></div>
</div>
</body>
<script>
var app = angular.module('AngularApp', []);
app.controller('AngularAppCtrl', function ($scope, $location, $http, $anchorScroll) {
//For fetching data from json
/* $http.get('anime.json').then(function(response){
$scope.anime = response.data.animes;
});*/

$scope.anime = ['Dragon Ball Z', 'One Punch Man', 'Naruto', 'One Piece', 'Yu Yu Hakusho', 'Tokyo Ghoul', 'Pokemon', 'Cowboy Bebop'];
$scope.addAnime = function () {
if (!$scope.newanime) { return; }
if ($scope.anime.indexOf($scope.newanime) == -1) {
/*var split = $scope.newanime.split(':');
$scope.anime.push({'anime': split[0],'hero':split[1]});*/
$scope.anime.push($scope.newanime);
$scope.newanime = "";
$scope.existError = false;
$location.hash('c');
$anchorScroll();
}
else {
$scope.existError = true;
}
};
$scope.removeErr = function () {
$scope.existError = false;
};
$scope.removeAnime = function (x) {
$scope.anime.splice(x, 1);
};
$scope.checkAnime = function () {
if ($scope.anime.indexOf($scope.newanime) == -1) {
$scope.existError = false;
}
else {
$scope.existError = true;
}
};
/*$scope.info = function (x,details) {
$scope.infoyes = true;
$scope.name = x;
$scope.inform = details;

};*/
$scope.info = function (x) {
$scope.infoyes = true;
$scope.name = x;

};
$scope.removeInfo = function () {
$scope.infoyes = false;
};
$scope.getIndex = function (x) {
return $scope.anime.indexOf(x);
}
});
</script>

</html>

最佳答案

您应该考虑使用 Javascript 方法 element.scrollIntoView();

可能是这样的:

$scope.addAnime = function() {
if (!$scope.newanime) {
newanime.scrollIntoView();
return;
}

关于javascript - 如何自动滚动到列表中最后插入的值(非底部)。[[注 :List is Sorted]],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54531771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com